Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to UIAlertView for iOS 9?

UAlertView is deprecated in iOS 9 and later. What would be an alternative?

UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[new show];
like image 963
user3138007 Avatar asked Oct 05 '15 21:10

user3138007


7 Answers

You can use this code to replace an alert view:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];           
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];

If you need multiple actions you can use:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Button 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // action 1
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Button 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // action 2
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    [self dismissViewControllerAnimated:YES completion:nil];
}]];           
[self presentViewController:alertController animated:YES completion:nil];
like image 153
Nithinbemitk Avatar answered Oct 14 '22 00:10

Nithinbemitk


You get often detailed information including the replacement suggestion by -clicking on the symbol which displays the class/method declaration.

In case of UIAlertView you will see

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

like image 35
vadian Avatar answered Oct 14 '22 01:10

vadian


  UIAlertController * alert=   [UIAlertController
                             alertControllerWithTitle:@"Info"
                             message:@"You are using UIAlertController"
                               preferredStyle:UIAlertControllerStyleAlert];

   UIAlertAction* ok = [UIAlertAction
                    actionWithTitle:@"OK"
                    style:UIAlertActionStyleDefault
                    handler:^(UIAlertAction * action)
                    {
                        [alert dismissViewControllerAnimated:YES completion:nil];

                    }];
  UIAlertAction* cancel = [UIAlertAction
                         actionWithTitle:@"Cancel"
                       style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action)
                       {
                           [alert dismissViewControllerAnimated:YES completion:nil];
                        }];
 [alert addAction:ok];
 [alert addAction:cancel];

 [self presentViewController:alert animated:YES completion:nil];
like image 21
Mr.Javed Multani Avatar answered Oct 14 '22 00:10

Mr.Javed Multani


I made a category for that:

+ (void)alertViewWithTitle:(NSString *)aTitle message:(NSString *)aMessage viewController:(UIViewController *) aVC
{
    UIAlertController * alert = [UIAlertController
                                 alertControllerWithTitle:aTitle ? aTitle : @""
                                 message:aMessage
                                 preferredStyle:UIAlertControllerStyleAlert];

    UIViewController *topVC = aVC ? aVC : [UIApplication sharedApplication].keyWindow.rootViewController;
    [topVC presentViewController:alert animated:YES completion:nil];
}

The parameters aTitle and aVC are optional but aVC should be used if known.

PS: avoid the "new" as a variable name this is a reserved word, I actually don't know if it will compile though.

like image 41
Rémy Blanc Avatar answered Oct 14 '22 00:10

Rémy Blanc


UIAlertController has been around since iOS 8.

like image 23
quark Avatar answered Oct 14 '22 00:10

quark


UIAlertController * alert=   [UIAlertController
                                    alertControllerWithTitle:@"My Title"
                                    message:@"Enter User Credentials"
                                    preferredStyle:UIAlertControllerStyleAlert];

    [self presentViewController:alert animated:YES completion:nil];
like image 36
user3138007 Avatar answered Oct 14 '22 02:10

user3138007


I have use "UIAlertController" on iOS 8 and later. Let see:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" preferredStyle:UIAlertControllerStyleAlert];

And add buttons:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
           //do something when click button
}];

Remember:

[alertController addAction:okAction];

Then show it:

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

If you want to show a actionsheep, you change

"preferredStyle:UIAlertControllerStyleActionSheet"
like image 35
AmyNguyen Avatar answered Oct 14 '22 00:10

AmyNguyen