gpt4 book ai didi

ios - 将方法添加到UINavigationBar后退按钮?

转载 作者:行者123 更新时间:2023-12-01 18:22:00 25 4
gpt4 key购买 nike

如何将方法添加到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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com