如何从另一个控制器运行此功能

时间:2016-12-08 21:29:38

标签: swift swift3

我在这个CV中有一个控制器(UICollectionView)我有一个更多的CV

    class HomeController: UICollectionViewController
    func handleSearch(){
     navigationController?.pushViewController(AlfavitController(), animated: true)}
    collectionView?.register(MainScreenCell.self, forCellWithReuseIdentifier: "mainCell")
    collectionView?.register(AlfavitCollectionCell.self, forCellWithReuseIdentifier: "alfavitCell")
    collectionView?.register(AvtorCollectionCell.self, forCellWithReuseIdentifier: "avtorCell")
    collectionView?.register(SaveCollectionCell.self, forCellWithReuseIdentifier: "saveCell")

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.item == 0 {
       return collectionView.dequeueReusableCell(withReuseIdentifier: "mainCell", for: indexPath)
    }
    if indexPath.item == 1 {
    return collectionView.dequeueReusableCell(withReuseIdentifier: "alfavitCell", for: indexPath)
    }
    if indexPath.item == 2 {
    return collectionView.dequeueReusableCell(withReuseIdentifier: "avtorCell", for: indexPath)
    }
    if indexPath.item == 3 {
    return collectionView.dequeueReusableCell(withReuseIdentifier: "saveCell", for: indexPath)
    }

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    return cell

在这个简历中我还有一个简历

    class AlfavitCollectionCell: UICollectionViewCell, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {

    lazy var mainCollectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let mc = UICollectionView(frame: .zero, collectionViewLayout: layout)
        mc.translatesAutoresizingMaskIntoConstraints = false
        mc.delegate = self
        mc.dataSource = self
        mc.backgroundColor = UIColor(red: 98/255, green: 172/255, blue: 238/255, alpha: 0)
    return mc
    }()

    var homeController:HomeController?



    func setupCollection(){

    addSubview(mainCollectionView)
    mainCollectionView.backgroundColor = UIColor(red: 98/255, green: 172/255, blue: 238/255, alpha: 1)

    mainCollectionView.register(AlfavitScreenCell.self, forCellWithReuseIdentifier: "cell")

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":mainCollectionView]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":mainCollectionView]))
    }

    override init(frame: CGRect) {
    super.init(frame: frame)

    setupCollection()
    }


    required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
    }



     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AlfavitScreenCell

    cell.cellLabel.text = DataBase.arLetter[indexPath.item]

    cell.backgroundColor = UIColor.white
    return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    homeController?.handleSearch()
    }

我如何运行homeController?.handleSearch()func in didSelectItemAt在Debugger homeController = nil?在哪里我必须委托或使用homeController做什么

2 个答案:

答案 0 :(得分:1)

有几种方法可以解决这个问题:

  • 在AlfavitCollectionCell上设置指向回家的委托。

示例:

 class AlfavitCollectionCell{
      var home:HomeController?
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          self.home?.handleSearch()
          ... 
       }
       ...
  }


 class HomeController: { 
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          var cell: AlfavitCollectionCell = ...
          cell.home = self
  • 或者您可以使用单例模式: 在HomeControllerstatic var instance:HomeController? 然后在......也许viewDidLoad中设置该实例。

答案 1 :(得分:0)

if indexPath.item == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "alfavitCell", for: indexPath) as! AlfavitController
cell.homeController = self
return cell
}