gpt4 book ai didi

iphone 导航 Controller : wait for uialertview response before to quit the current view

转载 作者:太空狗 更新时间:2023-10-30 03:41:14 24 4
gpt4 key购买 nike

我有一个带有导航 Controller 管理的后退按钮的 View ,我想检查当用户单击后退按钮时文件是否已保存。如果文件已保存,您返回上一个 View ,否则 uialertview 会询问您是否要保存文件。

所以我这样做了,但是 View 消失了,之后出现了警报 View 。

-(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 方法中的 navigationBar 属性上设置委托(delegate):

// 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;
}

然后在对话框的 YES 处理程序中,手动弹出 Controller :

[self.navigationController popViewController:YES];

关于iphone 导航 Controller : wait for uialertview response before to quit the current view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1396582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com