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

发布于 2024-12-05 00:31:31 字数 747 浏览 0 评论 0原文

我有一个 UIAlertView ,其中有“确定”和“取消”按钮。我想在按下 OK 按钮时呈现一个模式视图控制器。这是我到目前为止所做的:

  1. 创建 UIAlertView 框。实现了 UIAlertViewDelegate 协议。实现了 (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 方法。
  2. 在上述方法中,当 buttonIndex == 0 时,我尝试执行以下操作:

    if (buttonIndex == 0)
    {
        ModalViewController *mdvc = [[[ModalViewController alloc] initWithNibName:nil bundle:nil] autorelease];
        [自我呈现ModalViewController:mdvc动画:是];
    }
    

事实证明,模态视图不会自行呈现。我尝试了许多其他方法,但它们只是让它变得复杂,让我创建了很多不必要的变量。一定有更简单的方法。

一些额外信息:

  1. 如果无论如何,这是一个 OpenGL ES 应用程序。
  2. 如果我因按下 UIButton 而调用 [self presentModalController:],它确实会按预期工作 - 我会看到模态视图控制器。

I have a UIAlertView that has the buttons "OK" and "Cancel". I'd like to present a modal view controller when the OK button is pressed. Here's what I have done so far:

  1. Created the UIAlertView box. Implemented UIAlertViewDelegate protocol. Implemented (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex method.
  2. In the above method, when buttonIndex == 0, I'm trying to do something to the effect of:

    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.
  2. If I invoke [self presentModalController:] as a result of a UIButton press, it does work as expected - I see the modal view controller.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

明月夜 2024-12-12 00:31:31

我也遇到了这个问题。很明显,警报必须在模式出现之前消失,因此您正在寻找一种方法来知道警报已消失。浏览一下文档,有一个简单的方法。

而不是在调用时呈现您的模式:

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

使用:

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

这对我来说就像一个魅力。从文档中:

该方法在动画结束且视图显示后调用
隐藏。

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.

凉月流沐 2024-12-12 00:31:31

好吧...所以我自己解决了这个问题。正如我所说,这是在 OpenGL ES 应用程序中,因此我设置了一些全局布尔值并在drawFrame 方法中调用了[selfpresentModalViewController]。我知道这绝对不是最好的方法,但在我所处的时间紧迫的情况下,似乎没有更好的解决办法!

这个问题肯定与延迟有关,但 performSelector:withObject:afterDelay 似乎还不够!

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. 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!

花海 2024-12-12 00:31:31

的新方法

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

首先,将该代码移至名为 presentModal:
然后,在处理 UIAlertView 响应的方法中,调用这个新方法,如下所示

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

这将推迟 presentModal: 方法的执行,直到处理 UIAlertView 的方法返回之后以及 UIAlertView 从屏幕上删除之后。

First, move that code to a new method named 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

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.

因为看清所以看轻 2024-12-12 00:31:31

“取消”按钮的索引为 0,“确定”按钮的索引为 1。您确定正在对正确的按钮索引执行操作吗? (即,如果您按“取消”,它会显示模式吗?)。

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? (i.e. if you press 'Cancel' does it show the modal?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文