通过navigationController传递带有segue的数据 [英] Passing data with segue through navigationController

查看:92
本文介绍了通过navigationController传递带有segue的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从一个viewController推送到另一个。我已将viewController连接到另一个ViewController的navigationController,然后将标识符设置为showItemSegue。我在这两行中出错:

I'm trying to push data from one viewController to another. I've connected a viewController to another ViewController's navigationController and then set the identifier to "showItemSegue". I get an error in these two lines:

var detailController = segue.destinationViewController as ShowItemViewController
detailController.currentId = nextId!

图片说明:

代码:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {


    nextId = itemArray?.objectAtIndex(indexPath.row).objectForKey("objectId") as? NSString

    self.performSegueWithIdentifier("showItemSegue", sender: self)

}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    if (segue.identifier == "showItemSegue") {
        var detailController = segue.destinationViewController as ShowItemViewController
        detailController.currentId = nextId!
    }

}


推荐答案

segue的目标视图控制器是 UINavigationController 。您需要让它的顶视图控制器到达真正的目标视图控制器:

The destination view controller of the segue is the UINavigationController. You need to ask it for its top view controller to get to the real destination view controller:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showItemSegue" {
        let navController = segue.destination as! UINavigationController
        let detailController = navController.topViewController as! ShowItemViewController
        detailController.currentId = nextId!
    }
}

这篇关于通过navigationController传递带有segue的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆