iOS7和iOS8的segue.destinationViewController [英] iOS7 and iOS8 segue.destinationViewController

查看:102
本文介绍了iOS7和iOS8的segue.destinationViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的应用程序同时适用于iOS7和iOS8,并且我的视图控制器中的prepareForSegue方法遇到了问题。

I'm trying to make my app work with both iOS7 and iOS8 and I ran into a problem in my prepareForSegue methods in my view controllers.

在iOS8中 segue.destinationViewController 属于UINavigationController类,所以我使用 [[segue.desinationViewController viewControllers] objectAtIndex:0] 哪个工作正常,但iOS7中 segue.desinationViewController 属于CMAMyViewControllerClassName类,当我尝试发送时显然会抛出错误一个 viewContollers 消息。

In iOS8 segue.destinationViewController is of class UINavigationController, so I use [[segue.desinationViewController viewControllers] objectAtIndex:0] which works fine, but in iOS7 segue.desinationViewController is of class CMAMyViewControllerClassName, which will obviously throw an error when I try to send a viewContollers message to it.

我发现这个解决方案可行,但我想知道是否有更好的解决方案?除了后一篇文章,我还没有找到任何关于它的信息。如果没有正确的解决方案,我将创建一个获取正确视图控制器的方法;我只是想知道其他人是如何处理这种情况的。

I found this solution which will work, but I was wondering if there's a better solution? Other than the latter post I haven't been able to find anything about it. If there's not a "proper" solution, I'll create a method that gets the correct view controller; I was just wondering how other people handled this situation.

谢谢!

推荐答案

通过在尝试访问其中一个属性之前检查目标视图控制器的类来解决此问题:

This issue is resolved by checking the class of the destination view controller before trying to access one of its properties:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    DestController *vc = segue.destinationViewController;

    if ([dvc isKindOfClass:[DestController class]])
        dvc.propertyName = @"Property Value";
    else
        // do something else
}

取自主题的解决方案。

这篇关于iOS7和iOS8的segue.destinationViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆