Iphone:当 viewWillAppear 实现中显示警报时停止执行代码

发布于 2024-09-26 00:45:44 字数 527 浏览 0 评论 0原文

我有一个工作应用程序

在加载第一个视图之前,

    - (void)viewWillAppear
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyAppp" message:@"Application will connect to Internet. Continue?"
                                               delegate:self cancelButtonTitle:nil otherButtonTitles:@"No, quit", @"Yes", nil];
    [alert show];    
    [alert release];
}

,我在 viewWillAppear 方法中放置了一个警报:我可以正确单击两个按钮(是/否)...

但是...我希望停止代码执行并等待答案,但代码继续运行,连接到互联网并检索数据...

如何根据用户输入阻止视图加载?

I have a working application

before the first view is loaded, i put an alert in the viewWillAppear method:

    - (void)viewWillAppear
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MyAppp" message:@"Application will connect to Internet. Continue?"
                                               delegate:self cancelButtonTitle:nil otherButtonTitles:@"No, quit", @"Yes", nil];
    [alert show];    
    [alert release];
}

I can get the clicks on the two button (Yes/No) correctly...

But...I would like code execution to stop and wait for an answer, but instead the code goes on, connects to the internet and retrieves data...

How do I prevent a view to load, based on a user input?

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

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

发布评论

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

评论(4

蓦然回首 2024-10-03 00:45:44

viewWillAppear 是一个通知,允许您在视图显示之前完成一些操作,您无法避免此处视图的出现。您必须检查您的实施情况。

The viewWillAppear is a notification which allows you to complete some stuff before the view is shown, you can't avoid the appearing of the view here. You have to review your implementation.

め七分饶幸 2024-10-03 00:45:44

只需将一个 viewWillAppear 方法分解为两个方法即可。不要尝试在一大块连续代码中完成所有工作。

第一种方法将启动警报,然后退出/退出/返回。

第二种方法可以由警报按钮响应处理程序调用,然后仅在警报处理程序调用后、用户响应后才完成视图的加载。

您可能需要也可能不需要在第一个方法和第二个方法之间保存额外的状态信息(在额外的属性或实例变量中而不是方法局部变量中)。

Just break your one viewWillAppear method into two methods. Don't try to do it all in one chunk of sequential code.

The first method will launch the alert and then just exit/quit/return.

The second method can be called by the alert button response handler, and then finish loading the view only after it's been called by the alert handler, after the user has responded.

You may or may not have to save extra state information (in extra properties or instance variables instead of method locals) between the first and second methods.

优雅的叶子 2024-10-03 00:45:44

可以使用下面的内容,我知道它的小老问题,但可能对其他人有用
..

[alert show];

while ((!alert.hidden) && (alert.superview != nil))
    {
        [[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];

    }

the below can be used, i know its little old question but might be useful for others
..

[alert show];

while ((!alert.hidden) && (alert.superview != nil))
    {
        [[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];

    }
皓月长歌 2024-10-03 00:45:44

我通过在“ViewDidLoad”中显示答案来解决这个问题,让一个委托来获取按下了哪个按钮,然后仅在用户按下“是”时处理数据

I solved it by showing the answer in the "ViewDidLoad", got a delegate to get which button was pressed and then processing the data ONLY if the user pressed "Yes"

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