【问题标题】:pass data to view controller using storyboard segue iOS使用 Storyboard Segue iOS 将数据传递给视图控制器
【发布时间】:2013-11-02 04:47:14
【问题描述】:

我知道有很多关于此的帖子,但我已经尝试了所有方法,但没有任何效果。因此,我尝试将两个视图控制器之间的对象传递给嵌入在导航项中的 DBKIngredientsViewController。我有一个带有标识符“showIngredientsSegue”的推送序列到 DBKIngredientsViewController。我收到的错误信息是:

'NSInvalidArgumentException',原因:'-[DBKIngredientsViewController topViewController]:无法识别的选择器发送到实例 0x8a92450'

我要连接的视图控制器嵌入在导航控制器中,我认为这会搞砸它。这有什么办法?需要明确的是,DBKViewController 已经嵌入到导航控制器中,并且 push segue 推送的是 DBKViewController,而不是嵌入它的导航控制器。我尝试了不同的方法,但似乎都没有。

-(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;
    }
}

【问题讨论】:

    标签: ios uiviewcontroller segue uistoryboardsegue


    【解决方案1】:

    你确定segue.destinationViewControllerUINavigationController 吗?看起来它只是一个DBKIngredientsViewController,所以这应该可以工作:

    DBKIngredientsViewController *controller = (DBKIngredientsViewController *)segue.destinationViewController
    

    此外,如果DBKViewController 已经有一个导航控制器,那么如果您正在推送DBKIngredientsViewController,则不需要第二个。如果您以模态方式显示DBKIngredientsViewController,则只需要第二个。

    【讨论】:

    • 嗯,我试过了。这是错误:警告:在演示过程中尝试在 上演示
    • 那么问题是你如何呈现视图控制器。视图控制器是如何呈现的?您是同时关闭一个模态并推送还是同时推送两次?
    • 我猜是两次推动?第一个视图控制器被推到根导航视图控制器的顶部。然后我在 segue 中推动 DBKIngredientsViewController。
    • 是的,只需删除第二个UINavigationController,我已经更新了我的答案。
    • segue 是否因某种原因发生了两次?您是否也通过代码调用它,也许是在单元格选择上?
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多