iphone navigationController :在退出当前视图之前等待 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;
}
}
I have a view with a back button managed with a navigation controller and I want to check if a file has been saved when the user click on the back button.
If the file has been saved you go back in the previous view, else a uialertview ask you if you want to save the file or not.
So I did that but the view disapear and the alertview appear after.
-(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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当 viewWillDisappear 被调用时,已经太晚了。您应该尽早拦截后退按钮。我从来没有这样做过,但我的建议是在 viewDidAppear 方法中的 navigationBar 属性上设置委托:
不要忘记在 viewWillDisappear 中将其设置回:
然后拦截 shouldPopItem 方法:
并在对话框的 YES 处理程序中手动设置弹出控制器:
When viewWillDisappear is called, it's already too late. You should intercept the back button earlier on. I have never done it, but my suggestion is to set the delegate on the navigationBar property in your viewDidAppear method:
Don't forget to set it back in viewWillDisappear:
Then intercept the shouldPopItem method:
And in the YES handler for the dialog, manually pop the controller:
您必须子类化 UINavigationController 才能使其工作。然后覆盖 - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item 。
您应该设置您的视图控制器采用的自定义委托协议,如果您允许它弹出,请调用您的 [super navigationBar shouldPopItem:],否则,向上述方法返回 NO。
You must subclass UINavigationController for this to work. Then override - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item .
You should set up a custom Delegate protocol that your view controllers adopt and, if you allow it to pop, call your [super navigationBar shouldPopItem:], else, return NO to the above method.
添加一个左按钮项目不是更容易吗?如下所示:
Wouldn't it be easier just to add a left button item as in the following:
要跟进 nobre 响应,正如 Jon 提到的,最好的方法是子类化 UINavigationController。
实现此目的的最简单和最快的方法:
@interface YourViewController
#pragma mark - viewWillAppear
- (void) viewWillAppear:(BOOL)动画
{
((CustomNavigationController*)self.navigationController).customDelegate = self;
最后
也是重要的部分,删除委托(以避免在弹出时重新触发自己)并在 UIAlertViewDelegate
case kpopup_back :
{
if(buttonIndex != 0) //确定
{
((CustomNavigationController*)self.navigationController).customDelegate = nil;
[self.navigationController popViewControllerAnimated:YES];
}
}
它
在我这边完美地工作,希望它能有所帮助。
以下是来源:
CustomNavigationControllerDelegate.h
CustomNavigationControllerDelegate.m
To follow up on nobre response and as Jon mentionned it, the best way is to subclass UINavigationController.
The easiest way and fastest way to acheive this :
@interface YourViewController <CustomNavigationControllerDelegate>
#pragma mark - viewWillAppear
- (void) viewWillAppear:(BOOL)animated
{
((CustomNavigationController*)self.navigationController).customDelegate = self;
}
case kpopup_back :
{
if(buttonIndex != 0) //OK
{
((CustomNavigationController*)self.navigationController).customDelegate = nil;
[self.navigationController popViewControllerAnimated:YES];
}
}
break;
It works flawlessly on my side, hope it can help.
Here are the sources :
CustomNavigationControllerDelegate.h
CustomNavigationControllerDelegate.m