Iphone:当 viewWillAppear 实现中显示警报时停止执行代码
我有一个工作应用程序
在加载第一个视图之前,
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.
只需将一个 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.
可以使用下面的内容,我知道它的小老问题,但可能对其他人有用
..
the below can be used, i know its little old question but might be useful for others
..
我通过在“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"