如何将方法添加到UINavigationbar的后退按钮,所以每当我单击该后退按钮时,我都需要检查一些值并显示UIAlertView?有什么选择吗?
我尝试过这种方法,但对我有用
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//show alert
}
而且这种方法,但都没有
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
NSLog(@"hi");
}
最佳答案
是的你可以
在viedDidLoad中
UIBarButtonItem * backBtn = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBackToAllPets:)];
self.navigationItem.leftBarButtonItem = backBtn;
编写以下函数来检查条件
-(void)goBackToAllPets:(id)sender
{
if(/*check condition*/)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"message" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
alert.tag = 0;
[alert show];
}
else
{
[self.navigationController popViewControllerAnimated:YES];
}
}
关于ios - 将方法添加到UINavigationBar后退按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16786313/