我有一个导航控制器管理的后退按钮美景,我想检查文件是否已被保存在上的后退按钮,用户点击。 如果文件已经保存你回去以前的观点,否则一个UIAlertView中询问你是否要保存文件或没有。
所以,我这样做,但鉴于disapear和alertview后出现。
-(void)viewWillDisappear:(BOOL)animated {
if(!self.fileSaved){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Save the file?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
[alert show];
[alert release];
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
NSLog(@"NO");
break;
case 1:
NSLog(@"yes");
break;
default:
break;
}
}
当viewWillDisappear被调用时,它已经来不及了。 你应该早拦截上的后退按钮。 我从来没有做过,但我的建议是设置委托在你的viewDidAppear方法中的导航栏属性:
// save the previous delegate (create an ivar for that)
prevNavigationBarDelegate = self.navigationController.navigationBar.delegate;
self.navigationController.navigationBar.delegate = self;
不要忘记设置回在viewWillDisappear:
self.navigationController.navigationBar.delegate = prevNavigationBarDelegate;
然后拦截shouldPopItem方法:
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
if(!self.fileSaved) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Save the file?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
[alert show];
[alert release];
return NO;
}
if ([prevNavigationBarDelegate respondsToSelector:@selector(navigationBar:shouldPopItem:)])
return [prevNavigationBarDelegate navigationBar:navigationBar shouldPopItem:item];
return YES;
}
而在该对话框的是处理器,手动弹出控制器:
[self.navigationController popViewController:YES];
你必须继承UINavigationController的这个工作。 然后重写 - (BOOL)的导航栏(UINavigationBar的*)的导航栏shouldPopItem:(UINavigationItem *)的项目。 你应该设置你的视图控制器采用自定义委托协议,如果你允许它弹出,请致电[超级的导航栏shouldPopItem:],否则,返回NO上面的方法。
那岂不是更容易只是左键项目添加如下所示:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveThisDate)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
为了跟进诺布雷响应和乔恩mentionned它,最好的办法是子类的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
文章来源: iphone navigationController : wait for uialertview response before to quit the current view