iphone - 当点击 backBarButtonItem 时,有没有办法阻止 UIViewController 从 UINavigationController 的堆栈中弹出?

标签 iphone cocoa-touch uiviewcontroller uinavigationcontroller

我有一个带有 Root View Controller 的 UINavigationController,然后我将 UIViewController 推送到导航 Controller 的堆栈上。当用户点击 backBarButtonItem 时,如果满足某些条件,我希望能够弹出警报 View 并取消 View Controller 的弹出。例如,用户可以做出某些选择,但它们的某些组合可能无效,因此我想通知他们进行更改。

我知道我可以阻止用户做出无效组合,或者在选择无效组合时弹出警报 View ,但我不想这样做。用户可能正在更改选择,并且可能意识到某个组合无效,但我宁愿让他们选择一些使该组合无效的内容,然后更改其他内容(如果他们在尝试之前没有进行更改,则通知他们到上一个屏幕)。例如,如果我阻止他们选择构成无效组合的内容,那么他​​们可能必须在屏幕上向上滚动,更改某些内容,然后向下滚动,而不是先进行选择,然后向上滚动并更改某些内容。

使用 viewWillDisappear: 不起作用,因为虽然我可以生成警报 View ,但我无法找到防止弹出发生的方法。显示警报 View ,但 View Controller 仍然弹出,并且它们返回到 Root View Controller (显示警报 View )。

有什么办法可以防止爆音的发生吗?如果不是,这是否值得提交错误报告,或者这是不必要的和/或深奥的?

最佳答案

您可以用自己的后退按钮替换后退按钮,该按钮在 loadView 中调用您想要的方法

cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" 
                                    style:UIBarButtonItemStyleBordered 
                                    target:self
                                    action:@selector(onCancelButtonSelected:)];



self.navigationItem.leftBarButtonItem = cancelButton;

然后稍后

-(void)onCancelButtonSelected:(id)sender {

if (!hasSavedProduct) 
{
    cancelAlert = [[UIAlertView alloc] initWithTitle:@"Product not saved" 
                                             message:@"Exit without saving?" 
                                            delegate:self 
                                   cancelButtonTitle:@"Exit" 
                                   otherButtonTitles:@"Save", 
                                                        nil];
    [cancelAlert show];
    [cancelAlert release];
}

那就让他们走吧

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {



if ([cancelAlert isEqual:actionSheet]) 
{
    if (buttonIndex == 0)
    {
        NSLog(@"Fine. Exiting without saving");


        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        NSLog(@"Save here"); 

    }
}

关于iphone - 当点击 backBarButtonItem 时,有没有办法阻止 UIViewController 从 UINavigationController 的堆栈中弹出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2702319/

相关文章:

swift - 如何嵌入 View Controller

ios - 无法在iOS中设置准备segue

iphone - UITableView编辑疑惑

iphone - libarchive 解压到指定文件夹?

ios - 根据 iPad 或 iPhone 更改图像 CGRectMake

iphone - 如何将 UIScrollView 和文本字段放置在其中?

swift - UIAlertController 去新的 ViewController

ios - 标签栏问题: -[UILayoutContainerView hash]: message sent to deallocated instance

iphone - 为 UIScrollView 实现自定义缩放

objective-c - NSOutputStream 多次写入