![](/img/trans.png)
[英]Swift / pass data from UICollectionView to DetailView / Custom button
[英]Pass Data from UICollectionView in Swift 2
我的問題是我想將數據從集合視圖傳遞到新視圖(tableview,但我認為這不是必需的信息)
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let myVC = DetailsTableViewController()
myVC.movieTitle = movies[indexPath.row].movieTitle!
print (movies[indexPath.row].movieTitle!)
performSegueWithIdentifier("DetailSegue", sender: reuseIdentifier)
}
可以打印,但是我的movieTitle:字符串? 將在DetailsViewController上為NIL。
救命!
performSegueWithIdentifier
將實例化適當的目標控制器並顯示它。 由於從未顯示myVC
因此在此處實例化的DetailsTableViewController
。 您有幾種方法可以解決此問題:
如果要使用segues,則在didSelectItemAtIndexPath
觸發segue,並實現prepareForSegue
將數據傳遞到目標視圖控制器(在傳遞給該方法的UIStoryboardSegue
上可以使用destinationViewController
)。
或者,您可以避免使用segues並將myVC
直接推到當前導航控制器上來展示myVC
(如果存在,則可以調用self.navigationController.pushViewController(myVC, animated:true)
),將其展示為模式,或其他有意義的展示在您的應用中。
問題解決了。 謝謝
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "DetailSegue" {
let detailsVC = segue.destinationViewController as! DetailsTableViewController
if let cell = sender as? UICollectionViewCell, indexPath = collectionView!.indexPathForCell(cell) {
// use indexPath
detailsVC.movieTitle = movies[indexPath.row].movieTitle!
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.