当我使用prepareForSegue将值传递给目标控制器时,存在一些问题 [英] When I use prepareForSegue to pass value to the aim controller, there are some problems

查看:79
本文介绍了当我使用prepareForSegue将值传递给目标控制器时,存在一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我使用storyboard创建两个uiviewcontroller: firstviewcontroller secondviewcontroller 。并使用xcode将 uinavigationcontroller 嵌入 secondviewcontroller (编辑器 - >嵌入 - >导航控制器)。然后,我还想使用segue将值从第一个传递到第二个。有代码:

Firstly, I use storyboard to create two uiviewcontroller: firstviewcontroller and secondviewcontroller. And using xcode to embed a uinavigationcontroller into secondviewcontroller(editor --> embed in --> navigation controller). Then, I also want to use segue to pass value from first to second. there is the code:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"passValue"])
    {
        secondViewController *saveViewController = [segue destinationViewController];
        [saveViewController setValue:data forKey:@"data"];
    }
}

但是报告错误,日志是:
由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是密钥数据的密钥值编码兼容。'

But there will report error, the log is: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key data.'

但是,当我删除导航控制器时,我使用Xcode插入它并按代码定义导航栏,此问题将消失。我想原因是真正的destinationViewController是 navigationcontroller 而不是 secondviewcontroller 。因此,如果我想保留Xcode嵌入的导航控制器,如何解决这个问题?

However, when I delete the navigation controller that I use Xcode to insert it and define the navigation bar by code, this problem will disappear. I guess the reason is the real destinationViewController is navigationcontroller instead of secondviewcontroller. So if I want to reserve the navigation controller that is embedded by Xcode, how can fix this problem?

推荐答案

你是对的, destinationViewController 将是 UINavigationController ,其中嵌入了 secondViewController 。也许试试:

You're right, destinationViewController will be a UINavigationController you have secondViewController embeded in. Maybe try that:

secondViewController *saveViewController = [[segue destinationViewController] topViewController];

这篇关于当我使用prepareForSegue将值传递给目标控制器时,存在一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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