简体   繁体   English

UIAlertView +模态视图控制器不起作用

[英]UIAlertView + Modal View Controller doesn't work

I have a UIAlertView that has the buttons "OK" and "Cancel". 我有一个UIAlertView具有按钮“确定”和“取消”。 I'd like to present a modal view controller when the OK button is pressed. 按下“ OK按钮时,我想展示一个模态视图控制器。 Here's what I have done so far: 到目前为止,这是我所做的:

  1. Created the UIAlertView box. 创建了UIAlertView框。 Implemented UIAlertViewDelegate protocol. 实现了UIAlertViewDelegate协议。 Implemented (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex method. 已实现(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex方法。
  2. In the above method, when buttonIndex == 0 , I'm trying to do something to the effect of: 在上述方法中,当buttonIndex == 0 ,我正在尝试执行以下操作:

     if (buttonIndex == 0) { ModalViewController *mdvc = [[[ModalViewController alloc] initWithNibName:nil bundle:nil] autorelease]; [self presentModalViewController:mdvc animated:YES]; } 

As it turns out, the modal view does not present itself. 事实证明,模态视图本身并不存在。 I tried many other approaches but they are just making it complex and making me create a lot of unnecessary variables. 我尝试了许多其他方法,但它们只是使其变得复杂,并且使我创建了许多不必要的变量。 There MUST be an easier way. 必须有一个更简单的方法。

Some Extra Information: 一些额外的信息:

  1. If it matters in anyway, this is an OpenGL ES application. 无论如何,这都是OpenGL ES应用程序。
  2. If I invoke [self presentModalController:] as a result of a UIButton press, it does work as expected - I see the modal view controller. 如果我通过按下UIButton调用[self presentModalController:],它确实可以正常工作-我看到了模式视图控制器。

I was having this problem as well. 我也有这个问题。 It seems clear the alert must be gone before the modal can present, so you're looking for a way to know the alert is gone. 显然,必须在模态出现之前就必须删除警报,因此您正在寻找一种方法来知道警报已消失。 Skimming the docs, there is a simple way. 略读文档,有一种简单的方法。

Instead of presenting your modal when this is called: 而不是在调用时显示您的模态:

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex

Use: 采用:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

This worked like a charm for me. 这对我来说就像是一种魅力。 From the documentation: 从文档中:

This method is invoked after the animation ends and the view is hidden. 动画结束并隐藏视图后,将调用此方法。

Alright... so I fixed this myself. 好吧...所以我自己修好了。 As I said, this was in an OpenGL ES application, so I set some global bools and called the [self presentModalViewController] in the drawFrame method. 如我所说,这是在OpenGL ES应用程序中,因此我设置了一些全局布尔值,并在drawFrame方法中将其称为[self presentModalViewController] This is definitely not the best method, I know, but in the time-crunch I am in, there seems to be no better solution! 我知道,这绝对不是最好的方法,但是在时间紧迫的情况下,似乎没有更好的解决方案!

The issue is certainly delay-related but performSelector:withObject:afterDelay doesn't seem to be sufficient! 这个问题当然与延迟有关,但是performSelector:withObject:afterDelay似乎还不够!

First, move that code to a new method named presentModal: 首先,将代码移到名为presentModal:的新方法中presentModal:

- (void)presentModal:(UIViewController *)mvc {
    [self presentModalViewController:mvc animated:YES];
}


Then, in the method where you handle the response from the UIAlertView, call this new method, like this 然后,在处理UIAlertView响应的方法中,像下面这样调用此新方法

ModalViewController *mdvc = [[[ModalViewController alloc] 
                                           initWithNibName:nil 
                                                    bundle:nil] autorelease];
[self performSelector:@selector(presentModal:) 
           withObject:mdvc 
           afterDelay:0.0];


That will postpone the execution of the presentModal: method until after the method handling the UIAlertView has returned, and after the UIAlertView has been removed from the screen. 这将推迟执行presentModal:方法,直到返回处理UIAlertView的方法之后以及将UIAlertView从屏幕中删除之后。

The Cancel button has index of 0, the OK button will have index of 1. Are you sure you are doing the action on the correct button index? “取消”按钮的索引为0,“确定”按钮的索引为1。确定要对正确的按钮索引执行操作吗? (ie if you press 'Cancel' does it show the modal?). (即,如果您按“取消”,它是否显示模态?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM