[英]Pass data from CollectionView Delegate to another ViewController
I'm beginner on Swift. 我是Swift的初学者。
I have CollectionView
in my UIViewController
and I need to pass data when user clicks on CollectionViewCell
to another ViewController
. 我的
UIViewController
有CollectionView
,当用户单击CollectionViewCell
到另一个ViewController
时,我需要传递数据。 I tried to override prepareForSegue
but I notice that it called first then code on didSelectItemAtIndexPath
. 我尝试覆盖
prepareForSegue
但我注意到它先调用,然后在didSelectItemAtIndexPath
上进行编码。
My First ViewController Code: 我的第一个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
}
}
My Distination view controller code 我的Distination视图控制器代码
import UIKit
class DistinationViewController: UIViewController{
var itemId : String!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print(itemId)
}
}
The value of itemId
in DistinationViewController
is nil DistinationViewController
中itemId
的DistinationViewController
nil
Thanks 谢谢
You can try to change the view through code like : 您可以尝试通过以下代码更改视图:
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)
}
You need to do two things: 您需要做两件事:
Change your func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
to the following one: 将您的
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 } }
and your prepareForSegue
method will be like this 您的
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
from your View Controller and assign it an identifier
segue
并为其分配identifier
According to your comment you have 3 collectionView
so i guess you need 3 segues with 3 different identifiers. 根据您的评论,您有3个
collectionView
所以我想您需要3个带有3个不同标识符的segue。
Hope this helps! 希望这可以帮助! Happy to discuss if you have more questions.
如果您还有其他问题,很高兴与您讨论。
You can put everything in your prepareForSegue. 您可以将所有内容都放入prepareForSegue中。 Like this:
像这样:
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.