0

我正在尝试按下 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"];
}

}
4

2 回答 2

0

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

于 2012-04-29T18:24:15.813 回答
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]; 
    }

}

我在学习,。,..,

于 2012-04-30T04:55:10.247 回答