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

First, I use the storyboard to create two uiviewcontroller: firstviewcontroller and secondviewcontroller. And using xcode to embed the uinavigationcontroller in the secondviewcontroller (editor -> embed in -> navigation controller) . Then I also want to use segue to pass the value from the first to the second. there is a 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 be an error message, log: Application termination due to an undetected exception "NSUnknownKeyException", reason: "[setValue: forUndefinedKey:]: this class is not a key value compatible with encoding for key data. '

However, when I remove the navigation controller, I use Xcode to insert it and define the navigation panel by code, this problem will disappear. I think the reason is that the real destinationViewController is a navigation controller instead of a secondviewcontroller. Therefore, if I want to reserve a navigation controller built-in Xcode, how to solve this problem?

+6
source share
1 answer

You are right, destinationViewController will be the UINavigationController in which you included secondViewController. Perhaps try the following:

 secondViewController *saveViewController = [[segue destinationViewController] topViewController]; 
+12
source

Source: https://habr.com/ru/post/923265/


All Articles