【问题标题】:why UIAlertController in iOS7 received nil value?为什么 iOS7 中的 UIAlertController 收到 nil 值?
【发布时间】:2014-11-03 08:00:12
【问题描述】:

为什么 iOS7 中的 UIAlertController 收到 nil 值而需要呈现但在 iOS8 中工作很好,我知道这是因为 iOS7 不支持 UIAlertController 类吗?

UIAlertController *view=[UIAlertController
                        alertControllerWithTitle:@"Hello"
                        message:nil
                        preferredStyle:UIAlertControllerStyleAlert];

 [self presentViewController:view animated:NO completion:nil];

【问题讨论】:

标签: ipad ios7 ios8 uialertcontroller


【解决方案1】:

这些类仅适用于 iOS8 及更高版本。见class reference

【讨论】:

    【解决方案2】:

    为了在iOS 8 和更低版本中显示AlertView,您可以使用以下代码:

    if ([self isiOS8OrAbove]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                                 message:message
                                                                          preferredStyle:UIAlertControllerStyleAlert];
    
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction *action) {
                                                             [self.navigationController popViewControllerAnimated:YES];
                                                         }];
    
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
    } else {
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:title
                                                             message:message
                                                            delegate:nil
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles: nil];
        [alertView show];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    - (BOOL)isiOS8OrAbove {
        NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"8.0"
                                                                           options: NSNumericSearch];
        return (order == NSOrderedSame || order == NSOrderedDescending);
    }
    

    【讨论】:

    • 但我认为检查类比检查系统版本更好?
    【解决方案3】:

    您使用的操作系统版本可能低于 iOS 8

    如果您使用 Xcode 6 编译代码并使用 iOS 版本

    - (void)showXYZAlert
    {
        if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
        {
            // Handle UIAlertController
            [self showXYZAlertController];
        }
        else
        {
            // Handle UIAlertView
            [self showXYZAlertControllerView];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      相关资源
      最近更新 更多