为了跟进 nobre 响应,正如 Jon 提到的那样,最好的方法是继承 UINavigationController。
实现这一目标的最简单方法和最快方法:
- 在 Interface Builder 中修改导航控制器的类以从 CustomNavigationControllerDelegate 继承

- 在你的 UIViewController 中实现 CustomNavigationControllerDelegate 协议
@interface YourViewController <CustomNavigationControllerDelegate>
#pragma mark - UINavigationBar Delegate Methods
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancel otherButtonTitles:ok, nil];
alert.tag = kpopup_back;
[alert show];
return NO;
}
- 将您的控制器注册为代表
#pragma mark - viewWillAppear
- (void) viewWillAppear:(BOOL)animated
{
((CustomNavigationController*)self.navigationController).customDelegate = self;
}
- 最后也是重要的部分,删除委托(以避免在弹出时重新触发自己)并自己在 UIAlertViewDelegate 中弹出控制器
case kpopup_back :
{
if(buttonIndex != 0) //OK
{
((CustomNavigationController*)self.navigationController).customDelegate = nil;
[self.navigationController popViewControllerAnimated:YES];
}
}
break;
它对我来说完美无缺,希望它可以提供帮助。
以下是来源:
CustomNavigationControllerDelegate.h
#import <UIKit/UIKit.h>
@protocol CustomNavigationControllerDelegate <NSObject>
@optional
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
@end
@interface CustomNavigationController : UINavigationController
@property (nonatomic, retain) id<CustomNavigationControllerDelegate> customDelegate;
@end
CustomNavigationControllerDelegate.m
#import "CustomNavigationController.h"
@interface CustomNavigationController ()
@end
@implementation CustomNavigationController
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
if (_customDelegate && [_customDelegate respondsToSelector:@selector(navigationBar:shouldPopItem:)]) {
return [_customDelegate navigationBar:navigationBar shouldPopItem:item];
}
return YES;
}
@end