How to Fix Warning Attempt To Present UIAlertController in Swift
byWarning of attempt to present view controller is common occurrence when making an easily made mistake. I will take Alert controller for an example to discuss.
Lets’s create a scenario to bring UIAlertController warning message and observe side effect to understand our situation more effectively. Then we will understand on how to fix warning attempt to present UIAlertController. I will present a Detail view controller modally from Main controller, and Main controller will be delegate object assigned to the property of Detail view controller. In our presented view controller I have a dismiss code when back button is pressed. Before dismissing the controller I will call the delegate object - Main controller in this case - to present Alert controller from Main controller.
//MainViewController
func presentTestAlert(){
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
self.present(alert, animated: true, completion: nil)
}
//DetailViewController
func didTapBack(){
self.delegate?.presentTestAlert()
self.dismiss(animated: true, completion: nil)
}
//MainViewController
-(void)presentTestAlert(){
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
//DetailViewController
-(void)didTapBack{
if([self.delegate respondsToSelector:@selector(presentTestAlert)]){
[self.delegate presentTestAlert];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
You might also like: Sort an array of string containing numbers
Now: When I press a back button in Detail controller I see the warning attempt to present UIAlertController.Here’s the deal: This warning could be misinterpreted because of (null) information in the message. We may assume from the warning message that our modal controller – UIAlertController in this case – is null or the UIViewController from where we are trying to present UIAlertController is null. That is bit misleading to inattentive eye.
The warning message states that my Main Controller is already presenting some controller – Detail controller in this case – while I am making an attempt to present Alert Controller. I cannot present another controller while there is already a controller that is not dismissed yet. In our example I am calling my delegate object to present UIAlertController before dismissing the Detail controller. That is the most common easily made mistake.
What’s the bottom line? I will make sure that any controller is presented before is dismissed properly first.
You might also like: Blurry Pixel Font in SpriteKit bug
You might also like: Auto sizing UICollectionViewCell of UICollectionView fails in iOS 12