加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 在iOS 5应用程序中有条件支持iOS 6功能

发布时间:2020-12-14 19:54:48 所属栏目:百科 来源:网络整理
导读:如何在最小部署目标设置为iOS 5.0的应用程序中支持iOS6的功能? 例如,如果用户有iOS 5,他会看到一个UIActionSheet,如果用户有iOS 6,他会看到不同的iOS 6 UIActionSheet?你怎么做到这一点? 我有Xcode 4.5,想要在iOS 5上运行的应用程序. 解决方法 您应该总是
如何在最小部署目标设置为iOS 5.0的应用程序中支持iOS6的功能?

例如,如果用户有iOS 5,他会看到一个UIActionSheet,如果用户有iOS 6,他会看到不同的iOS 6 UIActionSheet?你怎么做到这一点?
我有Xcode 4.5,想要在iOS 5上运行的应用程序.

解决方法

您应该总是更喜欢检测可用的方法/功能,而不是iOS版本,然后假设一个方法可用.

见Apple documentation.

例如,在iOS 5中显示模态视图控制器,我们会这样做:

[self presentModalViewController:viewController animated:YES];

在iOS 6中,presentModalViewController:animated:UIViewController的方法是Deprecated,你应该使用presentViewController:animated:completion:在iOS 6中,但是你怎么知道什么时候使用什么?

您可以检测到iOS版本,并且如果您使用前者或后者,则使用if语句,但是,这很脆弱,您将犯错,可能未来的新操作系统将有一种新的方法来执行此操作.

处理这个问题的正确方法是:

if([self respondsToSelector:@selector(presentViewController:animated:completion:)])
    [self presentViewController:viewController animated:YES completion:^{/* done */}];
else
    [self presentModalViewController:viewController animated:YES];

你甚至可以争辩说你应该更加严格,并做一些事情:

if([self respondsToSelector:@selector(presentViewController:animated:completion:)])
    [self presentViewController:viewController animated:YES completion:^{/* done */}];
else if([self respondsToSelector:@selector(presentViewController:animated:)])
    [self presentModalViewController:viewController animated:YES];
else
    NSLog(@"Oooops,what system is this !!! - should never see this !");

我不确定您的UIActionSheet示例,据我所知,这在iOS 5和6上是相同的.也许您正在考虑使用UIActivityViewController进行共享,如果您使用的是iOS 5,您可能会想要回到UIActionSheet,所以你可以检查一个类是否可用,请参阅here如何操作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读