如果是iOS 8及以上版本,使用UIAlertController,否则使用UIAlertView。

27

由于UIAlertView现在已被弃用,我希望使用iOS 8中使用的UIAlertController。是否有一种方法可以在不破坏对iOS 7支持的情况下使用它?是否可以进行某种if条件检查以检查iOS 8,否则为iOS 7支持执行其他操作?


请注意 - 由于您的部署目标是iOS 7(或更早版本),因此您可以直接使用UIAlertView。这将使您的代码更简单。 - rmaddy
2
我不知道Swift有一个respondsToSelector... 另外,UIAlertView在iOS8中出现了问题,它不能滚动,而在iOS7中可以,这就是为什么我想尝试同时使用两者的原因。 - Rocky Pulley
1
UIAlertController 上的滚动也不起作用。 - jcesarmobile
11个回答

0

尝试下面的代码。它适用于iOS 8及以下版本,运行良好。

if (IS_OS_8_OR_LATER) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleCancel
                             handler:^(UIAlertAction *action)
                             {

                             }];
[alertVC addAction:cancelAction];

[[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{

}];
}
else{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
}

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