防止 leftBarButtonItem navigationItem 显示警报

Posted

技术标签:

【中文标题】防止 leftBarButtonItem navigationItem 显示警报【英文标题】:Prevent leftBarButtonItem navigationItem show alert 【发布时间】:2015-04-09 07:25:16 【问题描述】:

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

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

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】:

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

【讨论】:

以上是关于防止 leftBarButtonItem navigationItem 显示警报的主要内容,如果未能解决你的问题,请参考以下文章

ios:leftBarButtonItem.target 和 .action

LeftBarButtonItem 有两个按钮。一键不响应点击

为啥我的 leftBarButtonItem 在 iOS 11.4 上变得更小

如何设置大小 leftBarButtonItem?

我的自定义 UIButton 不能用于替换 leftBarButtonItem

以编程方式将 leftBarButtonItem 添加到应用程序