[英]UIAlertView + Modal View Controller doesn't work
我有一个UIAlertView具有按钮“确定”和“取消”。 按下“ OK
按钮时,我想展示一个模态视图控制器。 到目前为止,这是我所做的:
(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
方法。 在上述方法中,当buttonIndex == 0
,我正在尝试执行以下操作:
if (buttonIndex == 0) { ModalViewController *mdvc = [[[ModalViewController alloc] initWithNibName:nil bundle:nil] autorelease]; [self presentModalViewController:mdvc animated:YES]; }
事实证明,模态视图本身并不存在。 我尝试了许多其他方法,但它们只是使其变得复杂,并且使我创建了许多不必要的变量。 必须有一个更简单的方法。
一些额外的信息:
我也有这个问题。 显然,必须在模态出现之前就必须删除警报,因此您正在寻找一种方法来知道警报已消失。 略读文档,有一种简单的方法。
而不是在调用时显示您的模态:
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
采用:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
这对我来说就像是一种魅力。 从文档中:
动画结束并隐藏视图后,将调用此方法。
好吧...所以我自己修好了。 如我所说,这是在OpenGL ES应用程序中,因此我设置了一些全局布尔值,并在drawFrame方法中将其称为[self presentModalViewController]
。 我知道,这绝对不是最好的方法,但是在时间紧迫的情况下,似乎没有更好的解决方案!
这个问题当然与延迟有关,但是performSelector:withObject:afterDelay
似乎还不够!
首先,将代码移到名为presentModal:
的新方法中presentModal:
- (void)presentModal:(UIViewController *)mvc {
[self presentModalViewController:mvc animated:YES];
}
然后,在处理UIAlertView响应的方法中,像下面这样调用此新方法
ModalViewController *mdvc = [[[ModalViewController alloc]
initWithNibName:nil
bundle:nil] autorelease];
[self performSelector:@selector(presentModal:)
withObject:mdvc
afterDelay:0.0];
这将推迟执行presentModal:
方法,直到返回处理UIAlertView的方法之后以及将UIAlertView从屏幕中删除之后。
“取消”按钮的索引为0,“确定”按钮的索引为1。确定要对正确的按钮索引执行操作吗? (即,如果您按“取消”,它是否显示模态?)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.