问题
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