pass data to view controller using storyboard segue iOS

a 夏天 提交于 2019-12-20 01:40:05

问题


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

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