I've got this UIAlertController that I present modally on top of the topmost view controller like so:
UIViewController *top = [Utility topController];
UIAlertController *alertController
= [UIAlertController alertControllerWithTitle:@"Error"
message:@"input string too long"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:ok];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancel];
[top presentViewController:alertController
animated:true
completion:nil];
}
The way I find the topmost view controller is by looping through the presented view controllers and by making sure I don't present on top of an alert controller:
+ (UIViewController *)topController {
UIViewController *topController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topController.presentedViewController) {
if ([topController.presentedViewController class] == [UIAlertController class]) {
[topController.presentedViewController dismissViewControllerAnimated:YES completion:nil];
break;
}
topController = topController.presentedViewController;
}
return topController;
}
This is all well and good and whatever UIViewController the top controller is presents the UIAlertController as it's presentedViewController. When I tap ok or cancel, the UIAlertController is implicitly dismissed. However, I have a custom UINavigationController that it gets presented on top of and for some reason that UINavigationController also has it's overridden dismissViewControllerAnimated: get called.
- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
[super dismissViewControllerAnimated:flag completion:completion];
}
Why is it that that UINavigationController has it's presentedViewController dismissed as well as itself? I should add that I present the UINavigationController like so:
[self presentViewController:nav animated:YES completion:nil];
Why is it that dismissViewControllerAnimated: seems to get called twice? Shouldn't the implicit call to dismissViewControllerAnimated only get called on the UINavigationController's presentedViewController which is the UIAlertController?