1

如何将方法添加到 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");
    }
4

2 回答 2

3

是的,你可以在 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];
    }


}
于 2013-05-28T07:40:10.207 回答
1

假设您有两个控制器 - Controller1 和 Controller2。Controller2 从 Controller1 推送。因此,在从 Controller1 将 Controller2 推送到 navigationController 之前

Controller2 *controller2 = [[[Controller2 alloc]  init]autorelease];
self.navigationItem.hidesBackButton = YES;   

现在,在 Controller2 的 viewDidLoad: 方法中,添加以下代码片段

UIBarButtonItem *backBarButtonItem =[[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBackToAllPets:)]autorelease];
self.navigationItem.leftBarButtonItem = backBarButtonItem;

在 backButtonClicked 方法中,您可以执行您想要的检查。

于 2013-05-28T07:45:03.227 回答