Mat*_*ieu 6 iphone objective-c uinavigationcontroller uialertview
我有一个带有导航控制器管理的后退按钮的视图,我想检查用户单击后退按钮时是否已保存文件.如果文件已保存,则返回上一个视图,否则uialertview会询问您是否要保存文件.
所以我这样做了,但视图消失了,警报视图出现了.
-(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;
}
}
Run Code Online (Sandbox Code Playgroud)
调用viewWillDisappear时,已经太晚了.您应该在之前拦截后退按钮.我从来没有这样做过,但我的建议是在viewDidAppear方法中的navigationBar属性上设置委托:
// save the previous delegate (create an ivar for that)
prevNavigationBarDelegate = self.navigationController.navigationBar.delegate;
self.navigationController.navigationBar.delegate = self;
Run Code Online (Sandbox Code Playgroud)
不要忘记在viewWillDisappear中将其设置回来:
self.navigationController.navigationBar.delegate = prevNavigationBarDelegate;
Run Code Online (Sandbox Code Playgroud)
然后拦截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;
}
Run Code Online (Sandbox Code Playgroud)
在对话框的YES处理程序中,手动弹出控制器:
[self.navigationController popViewController:YES];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7475 次 |
| 最近记录: |