简体   繁体   中英

iOS: Custom back button doesn't work

I use FlatUIKit in my project. I have a custom Back button defined like this:

    [UIBarButtonItem configureFlatButtonsWithColor:[UIColor peterRiverColor]
                              highlightedColor:[UIColor belizeHoleColor]
                                  cornerRadius:3
                               whenContainedIn:[UINavigationBar class], nil];

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

And here is my clickBack

 -(void)clickBack{
[self.navigationController popToViewController:[self.navigationController.viewControllers     objectAtIndex:self.navigationController.viewControllers.count-1] animated:YES];

}

The button appears as I want it to but that's it. If I click on it, nothing happens.

Any idea what I did wrong? I'm pretty new to this and never replaced a back button.

You just mentioned in one of your comments that you presented the view controller modally, that's different. You'll want to release the modal view controller. Try this:

[self dismissModalViewControllerAnimated:YES];

The pop method is only going to work if the view you are releasing was pushed onto the navigation controller.

Here's a link to a modal view controller example.

http://timneill.net/2010/09/modal-view-controller-example-part-1/

在clickBack方法中使用此代码

[self.navigationController popViewControllerAnimated:YES];
[UIBarButtonItem configureFlatButtonsWithColor:[UIColor peterRiverColor]
                              highlightedColor:[UIColor belizeHoleColor]
                                  cornerRadius:3
                               whenContainedIn:[UINavigationBar class], nil];

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


 -(void)clickBack
{

[self.navigationController popToRootViewControllerAnimated:YES];

}

copy and paste this code as it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM