【问题标题】:Alternative to UIAlertView for iOS 9?iOS 9 的 UIAlertView 的替代品?
【发布时间】:2016-01-02 15:50:30
【问题描述】:

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

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

【问题讨论】:

  • 您有什么理由不只是查看UIAlertView 的文档(它告诉您确切的操作)或在发布此问题之前进行搜索?请在发帖前尝试寻找答案。
  • 如果您在他们的文档中找不到答案,最好将您的项目转移到 iOS 8。:P

标签: ios objective-c ios9 uialertview uialertcontroller


【解决方案1】:

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

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

【讨论】:

  • 这需要很多代码来实现......他们在想什么?不过,在没有委托的情况下处理响应的能力是一个很好的补充。
  • @Kundapra Hudga,你为什么选择对 [self presentViewController:alertController animated:YES completion:nil] 使用 dispatch_async; ?
  • 您正在使用 UIAlertController,请参阅 Swift AlertController 教程:iosdevcenters.blogspot.com/2016/03/…
  • 为什么从调度队列中调用presentViewController?
【解决方案2】:

您通常会通过-单击显示类/方法声明的符号获得包括替换建议在内的详细信息。

如果是UIAlertView,你会看到

“UIAlertView 已被弃用。请使用 UIAlertController 来代替 UIAlertControllerStyleAlert 的首选样式”

【讨论】:

    【解决方案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];
    

    【讨论】:

      【解决方案4】:

      我为此做了一个分类:

      + (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”作为变量名,这是一个保留字,我其实不知道它是否会编译。

      【讨论】:

        【解决方案5】:

        UIAlertController 自 iOS 8 以来一直存在。

        【讨论】:

          【解决方案6】:
          UIAlertController * alert=   [UIAlertController
                                              alertControllerWithTitle:@"My Title"
                                              message:@"Enter User Credentials"
                                              preferredStyle:UIAlertControllerStyleAlert];
          
              [self presentViewController:alert animated:YES completion:nil];
          

          【讨论】:

            【解决方案7】:

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

            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"
            

            【讨论】:

              猜你喜欢
              • 2020-02-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-12-17
              • 2012-03-04
              • 2018-06-27
              • 2020-03-18
              相关资源
              最近更新 更多