Swift 4.2 委托和协议不起作用



我有UIViewControllerprofileViewController)其中有collectionViewcollectionViewsegmentedControl headerCell,当segmentedControl值发生变化时,我想老虎delegate方法,但它不起作用,这是代码,这是collectioview headercell类和协议。

import UIKit
// Protocol
protocol headerCellSegmentedDelegate {
    func changeTabe(whichOne : String)
}
// HeaderCell Class
class HeaderProfileCollectionReusableView: UICollectionReusableView {
    var  headerDelegate : headerCellSegmentedDelegate?
    @IBAction func changeValue(_ sender: UISegmentedControl) {
        if sender.selectedSegmentIndex == 0 {
            headerDelegate?.changeTabe(whichOne: "0")
        }else{
             headerDelegate?.changeTabe(whichOne: "1")
        }
    }
  }

在具有collectionView的同一UIViewControllerprofileViewController

    // profileViewController 
import UIKit
import SideMenu
class profileViewController: UIViewController {
    //let headdercell = HeaderProfileCollectionReusableView()
    @IBOutlet weak var collectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
       // headdercell.headerDelegate = self //still not working with this too
        setupSideMenu()
        // Do any additional setup after loading the view.
    }
}
extension profileViewController : headerCellSegmentedDelegate {
    func changeTabe(whichOne: String) {
        print("which tab " + whichOne)
    }
}

我尝试使用类和弱的协议,但仍然不起作用。

首先是你的代码

//let headdercell = HeaderProfileCollectionReusableView()

不应该被评论。

其次,取消注释后,将 headerDelegate 值分配给 self。

headdercell.headerDelegate = self

最新更新