Does changing a button's title prevent further

2019-08-29 05:11发布

I have a bar button, let's call it button 1, which when clicked, programatically segues to a different page.
However, after clicking another button (button 2), button 1's title is changed.

From then on, button 1 no longer responds regardless of whether it's title has been changed back by clicking button 2 again.

I hope you understand, if not - I can clarify

code: (button 2 is edit) - (1 is info/add)

-(void)editButton:(id)sender 
{
    [self setEditing:(![self isEditing])];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                                                                              style: UIBarButtonItemStylePlain 
                                                                             target:self 
                                                                             action:@selector(edit:)];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add" 
                                                                              style: UIBarButtonItemStylePlain 
                                                                             target:self 
                                                                             action:@selector(infoToAdd:)];
}

-(void) edit:(UIBarButtonItem *) barBtnItem
{
    // if not editing
    if (![self isEditing]) {
        [self setEditing:YES];
        [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
        [self.navigationItem.leftBarButtonItem setTitle:@"Add"];
    } else {
        [self setEditing:NO];
        [self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
        [self.navigationItem.leftBarButtonItem setTitle:@"Info"];
    }
}

-(void) infoToAdd:(UIBarButtonItem *) barBtnItem
{
    // if not editing
    if (![self isEditing]) {
        [self.navigationItem.leftBarButtonItem setTitle:@"Info"];
    } else {
        [self.navigationItem.leftBarButtonItem setTitle:@"Add"];
    }
}    

-(void)infoAddButton:(id)sender
{
    if (self.tableView.isEditing) {
        [self performSegueWithIdentifier:@"segueToAdd" sender:self];
    } else {
        [self performSegueWithIdentifier:@"segueToInfo" sender:self];
    }
}

Edit:

It seems the function 'infoAddButton' is not even getting called (I used a breakpoint) when I press the button, after changing it's title.

1条回答
不美不萌又怎样
2楼-- · 2019-08-29 06:06

i think this is help you. here explain only left button same logic you can implement for right button

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu" style:UIBarButtonItemStyleBordered target:self action:@selector(dosomeTask:)];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"show" style:UIBarButtonItemStyleBordered target:self action:@selector(dosomeTask1:)];

isValid is a Bool type. so initially in your viewDidLoad set NO

-(void)dosomeTask:(id)sender{

    if (isValid) {
          self.navigationItem.leftBarButtonItem.title=@"Menu";
        isValid=NO;

        [self infomationShow];


    }else{
         self.navigationItem.leftBarButtonItem.title=@"show";
        isValid=YES;
        [self menuController];

    }

}

-(void)menuController{

     NSLog(@"menu");

}

-(void)infomationShow{

     NSLog(@"infomationShow");
}
查看更多
登录 后发表回答