撤消IBAction?

时间:2012-04-29 14:00:59

标签: iphone ios xcode ibaction nsundomanager

我正在尝试按下UIBarButtonItem并执行一个动作。当它发生时,我正在更改BarButtonItem标题的名称。这样,如果我再次点击它,我想撤消它执行的操作,而不是编写所有内容以将其更改回来。这是我的代码示例。

- (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"];
}

}

2 个答案:

答案 0 :(得分:0)

没关系,重新运行原始代码以“撤消”操作会更容易。

答案 1 :(得分: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(temp.titleLabel.text==@"Test1")
    {
       [temp setTitle:@"Test2" forState:UIControlStateNormal]; 

    }
    else if (temp.titleLabel.text==@"Test2")
    {
      [temp setTitle:@"Test1" forState:UIControlStateNormal]; 
    }

}

我正在学习,。,..,