0

Trying to replace alertview with alertviewcontroller, using exact example from developer documentation, but keep getting the error that presentViewController method is not found.

Here is my code. I've tried to replace "self" with a dozen variations, all of which lead to app crash.

NSString* messageString = @"This is my message";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title"                                                                     
message:messageString                                                                 
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
   NSLog(@"Ok button pressed");                                               
}];
[alertController addAction:defaultAction];
[self presentViewController:alertController animated:YES completion:nil];
Lamour
  • 3,002
  • 2
  • 16
  • 28
  • 2
    `UIAlertController` is a view controller. `presentViewController:animated:` is a method of `UIViewController`. You have to present the alert from a view controller. – rmaddy Oct 04 '17 at 21:58
  • Thanks Maddy I get that but how? I’ve tried replacing self with alertController.presentingViewController which works perfectly for a mail composer but fails in this case. – Andrew Slade Oct 04 '17 at 22:51
  • You have to call `presentViewController` on whatever view controller wants to display the alert. – rmaddy Oct 04 '17 at 22:52

0 Answers0