When I use prepareForSegue to pass value to the aim controller, there are some problems

萝らか妹 提交于 2019-12-06 02:09:28

问题


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"];
    }
}

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.'

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?


回答1:


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

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


来源:https://stackoverflow.com/questions/12034093/when-i-use-prepareforsegue-to-pass-value-to-the-aim-controller-there-are-some-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!