ios - 使用 Storyboard segue iOS 将数据传递给 View Controller

标签 ios uiviewcontroller segue uistoryboardsegue

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

'NSInvalidArgumentException', reason: '-[DBKIngredientsViewController topViewController]: unrecognized selector sent to instance 0x8a92450'

我要连接到的 View Controller 嵌入在导航 Controller 中,我认为它搞砸了。有什么办法解决这个问题?需要明确的是,DBKViewController 已经嵌入到导航 Controller 中,而 push segue 推送的是 DBKViewController,而不是嵌入它的导航 Controller 。我尝试过不同的方法,但似乎都不起作用。

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

IMG

最佳答案

你确定 segue.destinationViewController 是一个 UINavigationController 吗?它似乎只是一个 DBKIngredientsViewController 所以这应该可以工作:

DBKIngredientsViewController *controller = (DBKIngredientsViewController *)segue.destinationViewController

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

关于ios - 使用 Storyboard segue iOS 将数据传递给 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19739037/

相关文章:

ios - 在 Swift 中绘制 CGContext 后重绘它?

swift - UICollectionView 行为与 Subview 不同

ios - didSelectRowAtIndexPath 只被调用一次

ios - 在为 navController 设置动画时保留 UIView

ios - 在 SWIFT 中推送之前如何加载 View ?

ios - 通过动态原型(prototype)中的 segue 传递 myCell.textLabel?.text 值

ios - 使用 AFNetworking 2.0 的 SSL Pinning 允许 POST 请求,而不管使用的证书文件如何

ios - AVCapturesession - 捕获视频、麦克风和背景音频

iphone - 字符串加倍,精度丢失

ios - 如何在一个 UIViewController 中处理多个 View