UIAlertView for iOS 9的替代品?

时间:2015-10-05 21:09:38

标签: ios objective-c ios9 uialertview uialertcontroller

iOS 9及更高版本中不推荐使用

UAlertView。什么是另类?

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

7 个答案:

答案 0 :(得分:48)

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

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 :(得分:9)

您经常会收到详细信息,包括的替换建议 - 点击显示类/方法声明的符号。

如果是UIAlertView,您会看到

  

“UIAlertView已被弃用。请使用UIAlertController,并使用UIAlertControllerStyleAlert的preferredStyle”

答案 2 :(得分:3)

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

答案 3 :(得分:1)

UIAlertController自iOS 8以来一直存在。

答案 4 :(得分:1)

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

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

答案 5 :(得分: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];

如果要显示动作,请更改

"preferredStyle:UIAlertControllerStyleActionSheet"

答案 6 :(得分:1)

我为此做了一个分类:

+ (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];
}

参数aTitleaVC是可选的,但如果已知,则应使用aVC

PS:避免将“ new”作为变量名使用,这是保留字,我实际上不知道它是否可以编译。