maxim rad
maxim rad

Reputation: 67

How run this function from another controller

I have a controller(UICollectionView) in this CV i have One More 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

In this CV i have one more CV

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

How i run homeController?.handleSearch() func in didSelectItemAt In Debugger homeController = nil? Where i must delegate or somthing doing with homeController

Upvotes: 1

Views: 98

Answers (2)

Joe Daniels
Joe Daniels

Reputation: 1706

A couple ways you can attack this:

  • set a delegate on AlfavitCollectionCell that points back to home.

Example:

 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
  • or you can use the singleton pattern: in HomeController: static var instance:HomeController? and then set that instance in... maybe viewDidLoad.

Upvotes: 1

maxim rad
maxim rad

Reputation: 67

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

Upvotes: 0

Related Questions