Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 833
user1361168 Avatar asked Aug 20 '12 08:08

user1361168


1 Answers

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

secondViewController *saveViewController = [[segue destinationViewController] topViewController];
like image 186
Johnnywho Avatar answered Oct 22 '22 03:10

Johnnywho