为什么在iOS7中,UIAlertController收到了nil值?

3

为什么在iOS7中需要展示时,UIAlertController会收到nil值,但在iOS8中运作良好?我想知道这是不是因为iOS7不支持UIAlertController类?

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

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

UIAlertController是在iOS 8中引入的,无法在iOS 7中使用。对于iOS 7,您应该使用UIAlertView。 - Sreejith
https://dev59.com/pF8f5IYBdhLWcg3wB-3E - Sreejith
我创建了一个简单的包装类,可以与两者一起使用。它模仿了UIAlertController。https://github.com/Reggian/RAAlertController - reggian
3个回答

4

这个类仅适用于iOS8及以上版本。请参阅类参考


3
为了在iOS8及更低版本中显示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);
}

但我认为检查类比检查系统版本更好? - Deeper

0

你可能正在使用低于iOS 8的操作系统版本。

如果你使用Xcode 6编译代码,并且在iOS版本小于8.0的设备上运行,那么你将会遇到这个问题。我建议你采取以下措施:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接