防止 leftBarButtonItem navigationItem 显示警报

Prevent leftBarButtonItem navigationItem show alert

我有一个文本编辑视图,我想在编辑文本后返回根视图之前提示用户。

到目前为止我已经试过了。

self.navigationItem.leftBarButtonItem.title = @"Back";
self.navigationItem.leftBarButtonItem.tintColor = [UIColor grayColor];


-(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.
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Confirm Submission" message:@"Current Job Sheet Incomplete\n Please Confirm Your Submission" delegate:self cancelButtonTitle:@"Submit" otherButtonTitles:@"Cancel", nil];
        alert.tag = 1;
        [alert show];
    }
//    [super viewWillDisappear:animated];
}

尽管这会显示警报,但不会阻止用户被推送到根视图。

在 leftBarButton 上添加自定义按钮

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

-(void)navigationBackBtnTap{
         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Confirm Submission" message:@"Current Job Sheet Incomplete\n Please Confirm Your Submission" delegate:self cancelButtonTitle:@"Submit" otherButtonTitles:@"Cancel", nil];
         alert.tag = 1;
         [alert show];
}