kernix
kernix

Reputation: 8490

Stop Code Execution After Calling UIAlertView

How can I stop code execution after showing UIAlertView until user pressed on the OK button? If that's a problem, what are the workarounds?

Upvotes: 2

Views: 2640

Answers (2)

Venu Gopal Tewari
Venu Gopal Tewari

Reputation: 5876

It seems that you don't want to execute the code that is written just after your [alertview show] method. To implement this add those lines of code to a method and call that method in the following delegate of UIAlertView.

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if(buttonIndex == OKButtonIndex)
       {
         // Issue a call to a method you have created that encapsulates the 
         // code you want to execute upon a successful tap.
         // [self thingsToDoUponAlertConfirmation];
       }

    }

Now, if you are going to have more than one UIAlertView within your class, you'll want to make sure that you can easily handle each UIAlertView. You can do this with an NSEnum and a tag setting on UIAlertView.

If you have three alerts, at the top of your class declare an NSEnum before your @interface like so:

 // alert codes for alertViewDelegate // AZ 09222014
 typedef NS_ENUM(NSInteger, AlertTypes)
{
    UserConfirmationAlert = 1, // these are all the names of each alert
    BadURLAlert,
    InvalidChoiceAlert
}; 

Then, right before your [alert show], set the tag of the alert to be the displayed.

myAlert.tag = UserConfirmationAlert;

Then in your UIAlertDelegate, you can perform the desired method within a switch/case like so:

// Alert handling code
#pragma mark - UIAlertViewDelegate - What to do when a dialog is dismissed.
// We are only handling one button alerts here.  Add a check for the buttonIndex == 1
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (alertView.tag) {
        case UserConfirmationAlert:
            [self UserConfirmationAlertSuccessPart2];
            alertView.tag = 0;
            break;

        case BadURLAlert:
            [self BadURLAlertAlertSuccessPart2];
            alertView.tag = 0;
            break;

        case InvalidChoiceAlert:
            [self InvalidChoiceAlertAlertSuccessPart2];
            alertView.tag = 0;
            break;

        default:
            NSLog(@"No tag identifier set for the alert which was trapped.");
            break;
    }
}

Upvotes: 1

kernix
kernix

Reputation: 8490

Ended up using this:

...

[alert show];

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

    }

Upvotes: 7

Related Questions