【问题标题】:Different ViewControllers according to ios versions根据 ios 版本不同的 ViewController
【发布时间】:2013-12-04 08:53:15
【问题描述】:

我有一个UICollectionViewControllerTabBarViewController,适用于iOS 6.0 和更高版本的ios。如果设备的版本早于iOS 6.0,我想知道是否有任何方法可以使用不同的ViewController。例如,我可以将UITableView 用于使用iOS 6.0 之前版本的设备,将UICollection 用于使用iOS 6.0 和后续版本的设备吗?

我也试过PSTCollectionView,但它有一些问题。

我添加了一张显示我的故事板的图片。我的收藏在 TabBarController 中,如果设备在 iOS 6.0 之前使用,我想更改为 TableView。

【问题讨论】:

  • 请使用这个stackoverflow.com/a/5337804/1091539,这是这个问题的最佳答案
  • Check iPhone iOS Version的可能重复
  • @Mutawe 我在发布这个问题之前检查了这个。但我不知道根据版本将不同控制器处理成 TabbarViewController 的方式。这是我真正想要的。

标签: ios iphone objective-c uiviewcontroller versioning


【解决方案1】:

有一个非常简单的解决方案

例如,在您的 tabBarControllers 中,索引 2 用于 CollectionView,而索引 3 用于 TableViewController

只需在情节提要中完成所有必需的设置图标名称等

现在在您的 ApplicationDidFinishLaunchingWith 选项中执行此操作

我假设你的 tabbarController 是 rootViewContrller 这样做

UITabbarController *tabbarController = (UITabBarController *)self.window.rootViewController;
NSMutableArray *arrayControllers = [NSMutableArray arrayWithArray:tabbarController.viewControllers];
if (OlderVersion) {//Check
    [arrayControllers removeObjectAtIndex:2];
}else{
    [arrayControllers removeObjectAtIndex:3];
}
[tabbarController setViewControllers:arrayControllers];

【讨论】:

  • 谢谢 Raheel。这会起作用。所以如果我的 tabBarController 不是 rootViewController 我必须在 viewWillAppear 方法中执行此操作?
  • 没有得到这个场景,但是如果你的 tabbarController 不是 rootViewController 然后通过 StoryBoard Id 得到它,首先在 Identity Inspector 的 storyBoard 中设置 ID 然后 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryBoardName"捆绑:无]; UITabBarController *tabbarController = [storyboard instantiateViewControllerWithIdentifier:@"TabbarControllerStoryBoardId"];//storyboardId
【解决方案2】:

是的,你可以这样做。

用这个获取手机的版本:

[[UIDevice currentDevice] systemVersion]

然后,根据值加载正确的视图控制器。

【讨论】:

  • 感谢您的回复。我知道如何检查设备使用的 ios 版本,但我想知道如何处理情节提要中的控制器
  • 根据版本使用 performSegue
  • 我的 CollectionViewController 与 storyboard 中的另一个屏幕相连。我可以在 storyboard 中为旧版本设计另一个断开连接的 ViewController 并在代码中的某处调用它吗?
  • @RaheelSadiq 我不知道该怎么做,因为我的收藏在 TabBarViewController 中,它有自己的 segues。
  • @hoya21 我不知道你的 storyBoard 的设计,但如果你对 segues 感到不舒服,那么让 storyBoard 引用那个 ViewController 并按你喜欢的方式推送/呈现模式。请参阅此,以供此参考stackoverflow.com/questions/19700439/…
【解决方案3】:

您需要向视图控制器添加标识符

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
if(versionValue>=7)
{
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewControllerIOS7"];
}
else
{
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"viewControllerIOS6"];
}

希望对您有所帮助。

【讨论】:

  • 感谢您的宝贵时间。你能告诉我我必须在哪里使用它,因为我的 ViewController 在 TabBarViewController 中吗?
  • 您应该在创建 TabBarViewController 时使用它,当您将集合视图添加到标签栏时。我假设您使用故事板,但要完成这项工作,您必须重构代码并且必须以编程方式进行。
【解决方案4】:

是的,您可以通过检查您的版本的以下条件来做到这一点。

NSString *currSysVer = [[UIDevice currentDevice] systemVersion];

    float versionValue=[currSysVer floatvalue];

    if(versionValue>=6)
    {
        // do some stuff you want to do here
    }

希望对你有所帮助。

【讨论】:

  • 感谢您的回答@Manthan。你能帮我在代码的哪一部分执行此操作,因为我的 UICollectionViewController 在 TabBarViewController 中?
  • systemVersion 是一个浮点值!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
相关资源
最近更新 更多