UIAlertView/UIAlertController iOS 7和iOS 8兼容性

57

我正在使用Swift编写应用程序,并需要显示一个警告。该应用程序必须兼容iOS 7和iOS 8。由于UIAlertView已经被UIAlertController代替,那么如何在不检查系统版本的情况下检查UIAlertController是否可用?我听说过Apple建议我们不要检查设备的系统版本来确定API的可用性。

这是我在iOS 8上使用的代码,但在iOS 7上会崩溃并显示“dyld: Symbol not found: _OBJC_CLASS_$_UIAlertAction”:

let alert = UIAlertController(title: "Error", message: message, preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alert.addAction(cancelAction)
presentViewController(alert, animated: true, completion: nil)

如果我在iOS 8上使用UIAlertView,会收到此警告:警告:尝试从视图控制器<_UIAlertShimPresentingViewController: 0x7bf72d60>中解除显示,而此时正在进行显示或解除显示!

14个回答

0

如果你想要兼容iOS 7,那就不要使用UIAlertController。就这么简单。

UIAlertView并没有被替换,它仍然可以完美地工作,并将在可预见的未来继续工作。


1
但是当我在 iOS 8 设备上运行时,会收到以下警告:警告:尝试在演示或解除操作正在进行时从视图控制器<_UIAlertShimPresentingViewController:0x7bf72d60>中解除! - Shan
我正在滥用UIAlertView来实现一种密码双重输入的方式,即必须匹配两个编辑框。我通过向较大的视图添加子视图,然后将该视图作为警报视图的附件视图来实现这一点。不幸的是,视图的大小出了问题,第二个编辑框完全被裁剪掉了,按钮也是如此。 - William T. Mallard
我已经提交一个雷达反馈,关于UIActionSheet/UIAlertViews的子视图不再返回的问题。基本上被告知使用新的UIAlertController代替UIAlert/Action,因为UIAlert/Action已经被弃用。这并不是特别理想,因为您不能针对iOS 7,并期望在运行iOS 8的用户中获得相同的结果。我认为使其正确工作的唯一方法是检测新样式的能力,并以此应用更改。 - Bill Burgess
7
@Eric,这个东西虽然在文档中被标记为“Deprecated”,但是在头文件中故意没有被标记为“Deprecated”。通常情况下,这意味着苹果公司希望人们放弃它,但是他们还将继续维护UIAlertView大约5年左右。对于大多数应用程序来说,现在还不是切换到UIAlertController的时候。如果可能的话,苹果希望开发人员与iOS 7保持向后兼容性,而iOS 7没有UIAlertController。我不确定为什么我的评论会被踩,因为我只是陈述了一个事实。 - Abhi Beckert
2
我在iOS8下遇到了UIAlertView的兼容性问题。有时候我只能看到我的iPad(横屏)左侧2/3变暗。警告视图出现错位,好像设备认为它被旋转了。屏幕的右侧仍然接受触摸手势! - Bradley Thomas
显示剩余2条评论

-1

尝试以下代码。它适用于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];
}

为什么我的回答被踩了?如果有错,请指出来。谢谢。 - amitshinik

-2

从这个链接下载警报类,轻松地在iOS 6、7和8上使用它。

//Old code  
**UIAlertView** *alert=[[**UIAlertView** alloc]initWithTitle:@"FreeWare" message:@"Welcome to Common class" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

//New code 

**MyAlertView** *alert=[[**MyAlertView** alloc]initWithTitle:@"FreeWare" message:@"Welcome to Common class" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

-2
在iOS8中,有一个新的类UIAlertController取代了UIAlertViewUIActionSheet。从iOS8开始,请使用UIAlertController,而对于iOS8及之前的版本,请使用UIAlertView和UIActionSheet。我认为iOS8添加了size classes,可以改变UIAlertView的显示方向。请参见:https://github.com/wangyangcc/FYAlertManage

如果您能解释答案,那将对其他人有所帮助。 - Nilesh

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