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

时间:2019-07-16 07:45:46

标签: ios swift uitableview uicollectionview

我在嵌入UIButton的{​​{1}}内部发现了UITableViewCell(全部查看)。按下UICollectionView时,它应将在UIButton中找到的所有项目传递并列出到目的地UICollectionView

这是我的ViewController的代码,其中包含UITableViewCellUIButton

UICollectionView

如何使 @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 } (全部查看)显示在UIButton中找到的,指向目标UICollectionView的特定索引处的项目?

1 个答案:

答案 0 :(得分:0)

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
            }
        }