2016-12-08 2 views
1

У меня есть контроллер (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 

В этом резюме я больше один 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() 
    } 

Как я бегу homeController? .handleSearch() func in didSelectItemAt В отладчике homeController = nil? Где я должен делегировать или Somthing делать с HomeController

ответ

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 
  • или вы можете использовать одноплодный шаблон: в HomeController: static var instance:HomeController? , а затем установить этот экземпляр в ... возможно viewDidLoad.
+0

как установить делегат? Я не знаю, как это делается? Может быть, пример –

+0

@maximrad проверить мои правки. Имеет ли это смысл? –

0
if indexPath.item == 1 { 
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "alfavitCell", for: indexPath) as! AlfavitController 
cell.homeController = self 
return cell 
} 
Смежные вопросы