objective-c - 导航 Controller 中 "Going back"的监听器

标签 objective-c ios xcode ios5 uinavigationcontroller

我找到了这个问题的许多旧答案,但它们都是 2 年以上的。

我想为左键添加监听器——“后退”键。我想显示 UIAlertView,然后等待响应 - 是/否。

在更新的 SDK 中是否可行?

最佳答案

是的,您可以通过以下方式自己捕获和处理后退按钮事件来做到这一点:

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

您可以在方法中显示警报:

- (void)backButtonPressed:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notice" message:@"Going back?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok"];
[alert show];
}

然后在UIAlertDelegate方法中

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) { // Button index: Cancel = 0; Ok = 1; 
        [self.navigationController popViewControllerAnimated:YES];
    }
}

关于objective-c - 导航 Controller 中 "Going back"的监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10757115/

相关文章:

c++ - CMake 生成的 Xcode 项目无法找到从 Xcode 构建的二进制 "Products"

ios - 获取复杂的NSDictionary的内容

iOS : JSONModel : NSInvalidArgumentException

ios - 我们如何使用 AVSampleBufferDisplayLayer 来渲染 CMSampleBufferRef?

ios - 使用react-native-webengage 添加 FirebaseMessaging pod 时应用程序在加载时崩溃

ios - 如何快速在 UIImage 上绘制/涂鸦线?

python - 使用 Xcode 3.2 进行 PyObjC 开发

xcode - MAC Cocoa 应用程序中的扩展列表

iphone - UITapGestureRecognizer 忽略网络链接请求

ios - 图像适合 UICollectionView 单元格