2014-09-26 46 views
0

我正在實施用於富文本編輯的格式欄。其中我有兩個按鈕撤消和重做。我有兩個關於蘋果開發者的文檔,但不能提出重做的工作。 問題是我有一個UITextView。每當用戶寫入每個單詞時,都在[textView undoManager]中註冊爲撤消操作,如shouldChangeTextInRange目標c中的重做問題

當用戶單擊撤銷按鈕時,通過代碼[[_myTextView撤消管理器]撤消]成功完成。但是,當用戶單擊重做按鈕時,不會執行重做。

當用戶點擊這個重做像[[_myTextView undoManager] redoActionName]時,我甚至已經打印了名稱,並打印出Action的名稱「Change」。但TextView的文本沒有任何反應。

我已經搜索了很多,但在每種情況下人們使用Interface Builder進行撤消和自動重做,但我正在使用代碼。另外請注意,即使在Ipad上鍵盤上的內置按鈕重做也無法使用鍵盤按鈕撤消後。請引導我。

-(BOOL) textView: (UITextView *)textView shouldChangeTextInRange:(NSRange) range replacementText: (NSString *) text{ 

    [[textView undoManager] registerUndoWithTarget:self selector:@selector(revertTextView:) object:textView.attributedText]; 
    [[textView undoManager] setActionName:NSLocalizedString(@"Change",@" undo")]; 

} 

-(void) revertTextView: (NSAttributedString *) textViewAttString{ 

    _myTextView.attributedText = textViewAttributedString; 

} 

-(IBAction) undoClick{ 

    [[_myTextView undoManager] undo]; 

} 

-(IBAction) redoClick{ 

    [[_myTextView undoManager] redo]; 

} 

回答

0

不要叫任何寄存器
的TextView已經NSUndoManager和它的配置

你需要做的是

- (IBAction)undoPressed 
{ 
    [[self.myTextView undoManager] undo]; 
} 

- (IBAction)redoPressed 
{ 
    [[self.myTextView undoManager] redo]; 
} 
+0

我試着和它不工作。 [[self.myTextView undoManager] undo]相當於[[_myTextView undoManager] undo],因爲我通過Interface Builder連接了它。還**撤消工作正常,重做不起作用**。就像我寫** abcd **並按下撤消它變成了abc,但是當按下重做時它仍然是** abc **。我也嘗試過使用** [[self undoManager] undo] **,在這種情況下,也只能撤消作品。 – Sahar 2014-09-26 07:51:34

+0

只需刪除,textView:shouldChangeTextInRange:和revertTextView:,它將開始工作 – l0gg3r 2014-09-26 07:54:47

+0

我刪除了兩個,然後既不撤消也不重做。 – Sahar 2014-09-26 09:30:28