Swift 将数据从CollectionViewCell传递到第二个ViewController上的NavigationItem.title

Swift 将数据从CollectionViewCell传递到第二个ViewController上的NavigationItem.title,swift,collectionview,Swift,Collectionview,更新,请参阅下面我的评论,因为我做错了什么。我认为有人可能会从所有这些新手错误中受益:):我有一个带有节标题和单元标题(UILabels)的collectionView,它是通过cloudkit动态输入的。我能够让代码最终选择一个单元格,然后将单元格的标题发送到第二个ViewController的navigationItem.title属性 但是,现在第二个ViewController在第一次出现后正在重新加载。我将第一个CollectionViewController嵌入到导航控制器中。我在故

更新,请参阅下面我的评论,因为我做错了什么。我认为有人可能会从所有这些新手错误中受益:):我有一个带有节标题和单元标题(UILabels)的collectionView,它是通过cloudkit动态输入的。我能够让代码最终选择一个单元格,然后将单元格的标题发送到第二个ViewController的navigationItem.title属性

但是,现在第二个ViewController在第一次出现后正在重新加载。我将第一个CollectionViewController嵌入到导航控制器中。我在故事板中创建了一个从CollectionView中的原型单元到第二个ViewController的推送序列,并为序列提供了一个标识符。你知道为什么它在第一次出现后会再次加载第二个ViewController吗

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


     self.performSegueWithIdentifier("selected", sender: indexPath)


}


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

    if segue.identifier == "selected" {


        let indexPaths : NSArray = self.collectionView!.indexPathsForSelectedItems()
        let indexPath : NSIndexPath = indexPaths[0] as NSIndexPath

        let theSelectedItem = sections[indexPath.section].category[indexPath.item]


        let svc = segue.destinationViewController as TableViewControllerNew
        svc.navigationItem.title = theSelectedItem

        // I created the tableview controller in the storyboard, and then subclassed the UITableViewController, and set the storyboard tableview controller's class to the subclass in the identity inspector
    }

didSelect
中设置
self.navigationItem.title=…
。。。方法,然后将其重置为第一个VC的
视图中的旧标题。这将使您不必在第一个VC中创建全局变量,也不必将标题作为属性传递给第二个VC,尽管这两种解决方案都应该可以工作

谢谢!然而,就在你发表评论之前,我终于在这个网站上找到了一个足够相似的例子。但是现在我有一个新问题..请参阅上面的新注释/代码。感谢您的洞察力!没有关系!我解决了问题…新手犯了错误!我没有意识到,如果你在故事板中有一个片段,你不需要同时调用“performsguewithidentifier”。希望这篇文章能帮助别人。另外,在prepareForSegue方法中找到了代码的另一个版本,该版本更短。替换为这一行,而不是上面的indexPath和indexPath的两行:
let indexPath:nsindepath=self.collectionView!。indexPathForCell(发送方为CollectionViewCell)