![](/img/trans.png)
[英]Pass Json response from one Viewcontroller to another Viewcontroller and populate CollectionView
[英]Pass data from CollectionView Delegate to another ViewController
我是Swift的初學者。
我的UIViewController
有CollectionView
,當用戶單擊CollectionViewCell
到另一個ViewController
時,我需要傳遞數據。 我嘗試覆蓋prepareForSegue
但我注意到它先調用,然后在didSelectItemAtIndexPath
上進行編碼。
我的第一個ViewController代碼:
import UIKit
class ViewController: UIViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController : UICollectionViewDataSource,UICollectionViewDelegate{
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collCell", forIndexPath: indexPath) as! MainCollectionViewCell
...
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
itemId = items[indexPath.item].id
let distinationViewController = DistinationViewController()
distinationViewController.itemId = itemId
}
}
我的Distination視圖控制器代碼
import UIKit
class DistinationViewController: UIViewController{
var itemId : String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print(itemId)
}
}
DistinationViewController
中itemId
的DistinationViewController
nil
謝謝
您可以嘗試通過以下代碼更改視圖:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
itemId = items[indexPath.item].id
let distinationViewController = self.storyboard.instantiateViewControllerWithIdentifier("DestinationViewControllerStoryBoardID") as! DistinationViewController
distinationViewController.itemId = itemId
self.navigationController.pushViewController(distinationViewController, animated: true)
}
您需要做兩件事:
將您的func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
更改為以下內容:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { // Here you can check which Collection View is triggering the segue if collectionView == collectionView1 { self.performSegueWithIdentifier("fromFirstCollectionView", sender: nil); } else if collectionView == collectionView2 { // from other CollectionView } }
您的prepareForSegue
方法將如下所示
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Even here you can check for segue.identifiers if you have something to differentiate let dvc = segue.destinationViewController as! DestinationViewController dvc.itemId = "Hello" }
segue
並為其分配identifier
根據您的評論,您有3個collectionView
所以我想您需要3個帶有3個不同標識符的segue。
希望這可以幫助! 如果您還有其他問題,很高興與您討論。
您可以將所有內容都放入prepareForSegue中。 像這樣:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier{
switch identifier {
case "distinationViewController":
if let nextScene = segue.destinationViewController as? DistinationViewController {
if let indexPath = self.collectionView.indexPathsForSelectedItems.lastItem {
nextScene.itemId=items[indexPath.item].id
nextScene.delegate = self
}
}
default: break
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.