暂停代码执行,直到按下UIAlertView按钮? [英] Pause code execution until UIAlertView button is pressed?

查看:107
本文介绍了暂停代码执行,直到按下UIAlertView按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个方法向对象发送一条消息(你对此有何了解),并期望一个BOOL作为答案。但是,BOOL回答它的期望是基于在接收对象的方法中创建的UIAlertView的答案。但是,代码在等待用户回答UIAlertView时不会暂停。我的问题是:如何在方法的返回值中使用-alertView:clickedButtonAtIndex?

One of my methods sends a message to an object (what do you know about that), and expects a BOOL for an answer. However, BOOL answer it is expecting is based on the answer to a UIAlertView created in the receiving object's method. However, the code doesn't pause while waiting for the user to answer the UIAlertView. My problem is: how do I use -alertView:clickedButtonAtIndex in the method's return value?

这是消息运行的代码(在这种结构中,我期待navigateAwayFromTab改变基于UIAlertView中的用户输入,但它永远不会有机会):

Here's the code the message runs (in this construction, I was expecting navigateAwayFromTab to change based on the user input in the UIAlertView, but it never gets a chance):

- (BOOL)readyToNavigateAwayFromTab {
    NSLog( @"message received by Medical View");
    navigateAwayFromTab = NO;
    UIAlertView *alert = [[UIAlertView alloc]
           initWithTitle:@"Navigate Away From Tab?"
                 message:@"Navigating away from this tab will save your work."
                delegate:self
       cancelButtonTitle:@"Cancel"
       otherButtonTitles:@"OK", nil
    ];
    [alert show];
    [alert release];
    return navigateAwayFromTab;
}
#define CANCEL 0
#define OK 1
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if( buttonIndex == OK ) navigateAwayFromTab = YES;
}

我一直在阅读模式UIAlertView辩论,我同意苹果的实施 - 至少作为规则。但是,在这种情况下,我没有看到任何解决问题的方法,将代码放在-alertView:clickedButtonAtIndex中,因为我不需要运行基于UIAlertView的代码,我只需要阅读响应。关于如何达到我的监狱的任何建议?我已经在[alert show]之后尝试了一段时间循环,但随后警报甚至没有显示,并且出于多种原因我无法使用-viewWillDisapear。

I've been reading up on the modal UIAlertView debate, and I agree with apple's implementation - at lest as the rule. However, in this case I don't see any way of solving the problem by putting code in -alertView:clickedButtonAtIndex because I don't need to run code based on the UIAlertView, I just need to read the response. Any suggestions on how I can reach my gaol? I've tried a while loop after [alert show] already, but then the alert doesn't even show then, and for a number of reasons I can't use -viewWillDisapear.

编辑

对于那些在现代ios时代观看此问题的人来说,这个问题与ios 2相关

For those viewing this question in the modern ios era, this question pertained to ios 2

推荐答案

UIAlertView的节目不仅没有等待用户触摸按钮,它甚至不等待警报视图淡入视图。它会立即返回

Not only does UIAlertView's show not wait for the user to touch a button, it doesn't even wait for the alert view to fade into view. It returns immediately.

为您的班级添加一个标志。如果为NO,则从readyToNavigateAwayFromTab返回NO并显示警报。在clickedButtonAtIndex中,设置标志,以便readyToNavigateAwayFromTab知道返回YES。仍然在clickedButtonAtIndex中,从代码重试标签导航。

Add a flag to your class. If it's NO, return NO from readyToNavigateAwayFromTab and show your alert. In clickedButtonAtIndex, set the flag so that readyToNavigateAwayFromTab knows to return YES. Still in clickedButtonAtIndex, retry the tab navigation from code.

这篇关于暂停代码执行,直到按下UIAlertView按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆