سؤال

I am trying to press a UIBarButtonItem and perform an action, which it does. When It does I'm changing the name of the title of the BarButtonItem. That way if I hit it again I want to Undo the action it performed rather than coding everything out to change it back. Here is an example of my code.

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

}
هل كانت مفيدة؟

المحلول

Never mind, it's easier just to re-run the original code to "Undo" the action.

نصائح أخرى

try it if u want.

Add the UIButton Programmatically in viewDidLoad.,,.

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

}

and give the action like this,.,.,

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

}

i am learning,.,..,

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top