简体   繁体   中英

Why is UIAlertController not pausing code while it waits for answer?

My understanding is that normal behavior for a UIAlertController after being presented is to wait for the user to respond in other words pause other code.

In this case when the user clicks save, a UIAlertController is supposed to appear, ask the user a question about saving and then wait for the response. However, the code is continuing and the alert controller gets dismissed after about one second.

What could be wrong with the following that is preventing the code from pausing and causing the alert to disappear after about one second?

-(void) save {
if (listView.text.length>=1) {
[self fireAlertNewOrUpdate];
}
// save everything else
//dismiss view controller - this line of code may be dismissing the alert controller...
}

-(void) fireAlertNewOrUpdate {
    UIAlertController *listAlert = [UIAlertController alertControllerWithTitle:@"Save list as new?" message:nil preferredStyle:UIAlertControllerStyleAlert];
       
       UIAlertAction* yes = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                             [self saveNewList];
                                 
                             }];
       UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Update existing" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action)
                                {
                                   [self updateExisting];
                                }];
       [listAlert addAction:cancel];
       [listAlert addAction:yes];
       if ([listAlert respondsToSelector:@selector(setPreferredAction:)]) {
           [listAlert setPreferredAction:yes];
       }
       [self presentViewController:listAlert animated:YES completion:nil];
    
}

You've given the answer yourself, except that you've also hidden it from us. (Fortunately, you hinted at it in a comment.) It's because there's a line of code you've omitted, in save , where you dismiss the view controller . That view controller is the alert, so the alert appears in response to the call fireAlertNewOrUpdate and then immediately disappears again. In effect, you are saying present / dismiss in a single breath.

My understanding is that normal behavior for a UIAlertController after being presented is to wait for the user to respond in other words pause other code

No, not at all true. In fact, just the opposite. Your "other code" just goes right on even after the alert has appeared. There is basically nothing in iOS programming where code will spontaneously "pause". For this reason, it is quite usual after you call present to present a view controller to do nothing further in that code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM