johnmm johnmm - 7 months ago 42
iOS Question

Is there a way to stagger when multiple Alert Views show up in the same View Controller?

So I have two different UIAlertViews in the same view controller and both alerts can be triggered at the same time. When both alerts are triggered, both alerts pop up at the same time, with the alerts being layered on top of each other. Is there a way to stagger the alerts so that when the first alert comes up, the second alert will not pop up until the user dismisses the first alert? For my code, this is the format I'm using

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ERROR!"
message:@"Error message here!"
[alertView show];


try the following:

create two properties

@property (weak, nonatomic) UIAlertView *visibleAlertView;
@property (strong, nonatomic) UIAlertView *pendingAlertView;

every time when you want to present an alertview from your code make a check

UIAlertView *newAlertView = [[UIAlertView alloc] init...
if (self.visibleAlertView) {
    self.pendingAlertView = newAlertView;
} else {
    self.visibleAlertView = newAlertView;
    [newAlertView show];

and finally:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (self.pendingAlertView) {
        UIAlertView *newAlertView = self.pendingAlertView;
        self.pendingAlertView = nil;
        self.visibleAlertView = newAlertView;
        [newAlertView show];

hope that helps :)


you could even stack the pending alertviews:

@property (strong, nonatomic) NSMutableArray *pendingAlertViews;
self.pendingAlertViews = [NSMutableArray array];

before presenting an alertview:

UIAlertView *newAlertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
if (self.visibleAlertView) {
    [self.pendingAlertViews addObject:newAlertView];
} else {
    self.visibleAlertView = newAlertView;
    [newAlertView show];

and in dismiss:

if (self.pendingAlertViews.count > 0) {
    UIAlertView *av = self.pendingAlertViews.firstObject;
    [self.pendingAlertViews removeObjectAtIndex:0];
    self.visibleAlertView = av;
    [av show];

hope it helps :)