Home » iOS programming » From the UIAlertView to the UIAlertController (updated)

From the UIAlertView to the UIAlertController (updated)

From iOS8 Apple provides the new UIAlertController class which you can use instead of UIAlertView which is now deprecated, it is also stated in deprecation message:

UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

OBJECTIVE C

This is an UIAlertView example of the iOS 7 style:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Email error!” message:@”error in sending email” delegate:nil  cancelButtonTitle:@”Ok”  otherButtonTitles:nil];

       [alert show];

        [alert release];

      The corresponding UIAlertController implementation is:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@”Email error!” message:@”error in sending email” preferredStyle:UIAlertControllerStyleAlert];

        [alertController addAction:[UIAlertAction actionWithTitle:@”Ok” style:UIAlertActionStyleDefault handler:nil]];

        [self presentViewController:alertController animated:YES completion:nil];

SWIFT

The old syntax was:

//let sendMailErrorAlert = UIAlertView(title: “Could Not Send Email”, message: “Your device could not send e-mail.  Please check e-mail configuration and try again.”, delegate: self, cancelButtonTitle: “OK”)

            //sendMailErrorAlert.show()

this old syntax can be replaced by:

let alert = UIAlertController (title: “Could Not Send Email”, message: “Your device could not send e-mail. Please check e-mail configuration and try again.”, preferredStyle: UIAlertController.Style.alert)

            alert.addAction (UIAlertAction(title: “Ok”, style:UIAlertAction.Style.default, handler:nil))

            self.present(alert, animated:true, completion:nil)

Leave a Reply