繁体   English   中英

Swift 4.2委托和协议无效

[英]Swift 4.2 Delegate and Protocol no working

我有UIViewControllerprofileViewController ),其中有collectionViewcollectionViewheaderCell ,其中有segmentedControl ,当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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM