问题
I know there are lots of postings about this but I have tried everything and nothing has worked. So I have tried to pass an object between two view controllers, to a DBKIngredientsViewController embedded in a navigation item. I have a push segue with the identifier "showIngredientsSegue" to the DBKIngredientsViewController. The error message I receive is:
'NSInvalidArgumentException', reason: '-[DBKIngredientsViewController topViewController]: unrecognized selector sent to instance 0x8a92450'
The view controller to which I am segueing is embedded in a navigation controller, which I think is messing it up. What's the way around this? To be clear, the DBKViewController is already embedded in a navigation controller, and the push segue pushes the DBKViewController, not the navigation controller embedding it. I have tried it different ways but none seem to work.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showIngredientsSegue"]){
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
DBKIngredientsViewController *controller = (DBKIngredientsViewController *)navController.topViewController;
controller.targetRecipe = selectedRecipe;
}
}
回答1:
Are you sure that segue.destinationViewController
is a UINavigationController
? It seems like it's just a DBKIngredientsViewController
so this should work:
DBKIngredientsViewController *controller = (DBKIngredientsViewController *)segue.destinationViewController
Also if DBKViewController
already has a navigation controller then you do not need a second one if you are pushing DBKIngredientsViewController
. You would only need a second one if you are modally displaying DBKIngredientsViewController
.
来源:https://stackoverflow.com/questions/19739037/pass-data-to-view-controller-using-storyboard-segue-ios