开发者

Iphone: stop execution of code when alert is shown in a viewWillAppear implementation

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 开发者_Go百科the code goes on, connects to the internet and retrieves data...

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


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.


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

[alert show];

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

    }


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"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜