Prior to iOS 8, a UIAlertView could be displayed from a modally presented UIViewController at the same time that the UIViewController was dismissed. I found this to be especially useful when a user needed to be alerted to some change that had taken place when they pressed the 'Save' button on the modally presented controller. Since iOS 8, in the case that a UIAlertController is displayed from a modally presented view controller while it is being dismissed, the UIAlertController is also dismissed. The UIAlertController gets dismissed before the user can read it or dismiss it himself. I know that I can have a delegate for the modally presented controller display the alert view once the controller is dismissed, but that case creates a ton of extra work since this controller is used in many places, and the UIAlertController must be presented with certain conditions, requiring parameters to be passed back to the controller delegate in each case. Is there any way to display a UIAlertController from a modally presented controller (or at least from the code within the controller) at the same time that controller is being dismissed, and have the UIAlertController stay until it is dismissed?
5
votes
1 Answers
2
votes
You can handle this in the completion block of dismissViewControllerAnimated method of your modal controller clas. Present the UIAlertController on the rootviewcontroller which should be handled in any class.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationItem.rightBarButtonItem setAction:@selector(dismissView)];
[self.navigationItem.rightBarButtonItem setTarget:self];
}
- (void)dismissView {
[self dismissViewControllerAnimated:YES completion:^{
[self showAlert];
}];
}
- (void)showAlert {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"This is Alert" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:okButton];
[alertController addAction:cancelButton];
UIViewController *rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
[rootViewController presentViewController:alertController animated:YES completion:nil];
}