2012-04-29 77 views
0

我想按下一個UIBarButtonItem並執行一個操作。當它這樣做時,我正在更改BarButtonItem的標題的名稱。這樣,如果我再次擊中它,我想撤消它執行的操作,而不是將所有操作都改回來。這是我的代碼的一個例子。撤銷IBAction?

- (IBAction)MyAction:(id)sender{ 

if([[MyButton title] isEqualToString:@"Test1"]){ 

//My Action is performed. 

    [MyButton setTitle:@"Test2"]; 
    [[undoManager prepareWithInvocationTarget:self] MyAction:?]; 
    [undoManager setActionName:@"UndoLastAction"]; 


}else if ([[MyButton title] isEqualToString:@"Test2"]){ 

    [MyButton setTitle:@"Test1"]; 
    [[undoManager prepareWithInvocationTarget:self]MyAction:?]; 
    [undoManager setActionName:@"UndoLastAction"]; 
} 

} 
+0

嘗試尋找一個紀念品設計模式,我認爲它就是你正在尋找 – Novarg 2012-04-29 14:11:09

+0

你有一個很好的鏈接? – Jason 2012-04-29 14:17:02

+0

@Novarg你願意提供一個例子嗎? – Jason 2012-04-29 18:17:44

回答

0

不要緊,只需重新運行原始代碼以「撤消」操作即可。

0

試試吧,如果你想。

以編程方式在viewDidLoad中添加UIButton。,,.

- (void)viewDidLoad 
{ 
MyButton =[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[MyButton setFrame:CGRectMake(300, 450, 72, 37)]; 
[MyButton setTitle:@"Test1" forState:UIControlStateNormal]; 
[MyButton addTarget:self action:@selector(MyAction:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view MyButton]; 

} 

,並給這樣的動作,。,。,

-(IBAction)MyAction:(id)sender 
{ 
    UIButton *temp=(UIButton *)sender; 

    if([email protected]"Test1") 
    { 
     [temp setTitle:@"Test2" forState:UIControlStateNormal]; 

    } 
    else if ([email protected]"Test2") 
    { 
     [temp setTitle:@"Test1" forState:UIControlStateNormal]; 
    } 

} 

我正在學習,。,..,