简体   繁体   English

如何使“全部查看”按钮通过segue到下一个ViewController显示数据?

[英]How do I make a “View All” button display data via segue to the next ViewController?

I have a UIButton (View All) found inside a UITableViewCell in which a UICollectionView is embedded. 我在嵌入了UICollectionViewUITableViewCell内部找到了一个UIButton (全部查看)。 When the UIButton is pressed, it should pass & list all the items found in the UICollectionView to the destination ViewController . 按下UIButton ,应将在UICollectionView找到的所有项目传递并列出到目标ViewController

Here is the code for my UITableViewCell containing the UIButton & UICollectionView : 这是我的UITableViewCell的代码,其中包含UIButtonUICollectionView

 @IBOutlet weak var ViewAllButton: UIButton!

 @IBOutlet weak var EventCollection: UICollectionView!

 var events = [Events]()

 override func awakeFromNib() {
        super.awakeFromNib()
  ViewAllButton.setIcon(prefixText: "View All ", prefixTextColor: .blue, icon: .typIcons(.chevronRight), iconColor: .blue, postfixText: " ", forState: .normal, iconSize: 24)
 }

 extension PopularCell: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return events.count
   }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = EventCollection.dequeueReusableCell(withReuseIdentifier: "EventCell", for: indexPath) as! EventCell
            let event = events[indexPath.row]
            print("Event Name:\(event.event_name)")
             cell.event = event
         cell.tag = indexPath.row
            return cell

    }

How do I go along to make the UIButton (View All) display the items found in the UICollectionView at a particular index to the destination ViewController ? 如何使UIButton (“查看全部”)显示在UICollectionView中找到的项,并UICollectionView目标ViewController的特定索引?

1. Add the following code with in a new file named UIView_Ext:

    extension UIView {
        var parentViewController: UIViewController? {
            var parentResponder: UIResponder? = self
            while parentResponder != nil {
                parentResponder = parentResponder!.next
                if let viewController = parentResponder as? UIViewController {
                    return viewController
                }
            }
            return nil
        }
    }

2. On View All button Action in the tableViewCell do the following:

    self.parentViewController?.performSegue(withIdentifier: "Segue_Identifer", sender: self.events)

3. Implement prepare for segue in the view controller you are having table view controller reference as below:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "Segue_Identifier" {
                let destinationViewController = segue.destination as! DestinationViewController
                destinationViewController.events = events
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM