阻止leftBarButtonItem navigationItem显示警报

时间:2015-04-09 07:25:16

标签: ios objective-c uinavigationcontroller uinavigationitem

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

到目前为止我已尝试过这个。

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];
}

虽然这显示警告,但它不会阻止用户被推到根视图。

1 个答案:

答案 0 :(得分:1)

在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];
}