2015-10-05 39 views
15

UAlertView已在iOS 9及更高版本中弃用。什么是替代方案?UIAlertView for iOS 9的替代方案?

UIAlertView *new = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your InApp Purchases were successfully restored" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[new show]; 
+1

任何原因,你没有简单地看在'UIAlertView'(它告诉你到底该怎么做)的文档中,或者在发布这个问题之前做一个搜索?请在发布前尝试找到答案。 – rmaddy

+0

如果您在他们的文档中找不到答案,请将您的项目更好地转移到iOS 8.:P –

回答

45

您可以使用此代码来替换警报视图:

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]; 

如果需要多个动作,你可以使用:

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]; 
+1

这需要执行很多代码......他们在想什么?虽然没有委托处理响应的能力是一个很好的补充。 –

+0

@Kundapra Hudga,你为什么选择使用dispatch_async [self presentViewController:alertController animated:YES completion:nil]; ? – Adela

+0

您正在使用UIAlertController,请参阅Swift AlertController教程:https://iosdevcenters.blogspot.com/2016/03/uialertcontroller-in-swift.html –

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

    [self presentViewController:alert animated:YES completion:nil]; 
9

你得到经常的详细信息,包括通过更换建议-clicking上显示的类/方法声明的符号。

UIAlertView情况下,你会看到

“UIAlertView中已被弃用。使用UIAlertController与UIAlertControllerStyleAlert的preferredStyle而不是”

1

我有使用 “UIAlertController” 在iOS 8及更高版本。让我们看到:

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

并添加按钮:

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

记住:

[alertController addAction:okAction]; 

然后告诉它:

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

如果你想显示actionsheep,你更改

"preferredStyle:UIAlertControllerStyleActionSheet" 
2
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];