#selector未解析的标识符函数

时间:2018-12-18 03:33:26

标签: swift

在init函数中,#selector似乎找不到调用函数。

这是班上

import UIKit

protocol ExpandableHeaderViewDelegate {
func toggleSection(header: ExpandableHeaderView, section: Int)
}

class ExpandableHeaderView: UITableViewHeaderFooterView {
var delegate: ExpandableHeaderViewDelegate?
var section: Int!

@IBOutlet weak var titleLabel: UILabel!

  override init(reuseIdentifier: String?){
    super.init(reuseIdentifier: reuseIdentifier)
    self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderView(_:))))

}

@objc func selectHeaderView(gesture: UITapGestureRecognizer){
    let cell = gesture.view as! ExpandableHeaderView
    delegate?.toggleSection(header: self, section: cell.section)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderView(_:))))
}


func custonInt(title: String, section: Int, delegate:   ExpandableHeaderViewDelegate){

    self.titleLabel.text = title
    self.section = section
    self.delegate = delegate
}

override func layoutSubviews(){
    super.layoutSubviews()
    self.titleLabel.textColor = UIColor.white
    self.contentView.backgroundColor = UIColor.darkGray
}

}

这是错误:

Use of unresolved identifier 'selectHeaderView'

init函数添加手势识别器找不到selectHeaderView。它的表现就好像它不是课程的一部分。我在做什么错了?

2 个答案:

答案 0 :(得分:1)

更改此:

self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderView(_:))))

收件人:

self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderView(gesture:))))

答案 1 :(得分:0)

您的类确实没有名为selectHeaderView(_:)的方法。 selectHeaderView(_:)。但是,您的课程确实具有方法selectHeaderView(gesture:)。在这种情况下,参数标签确实很重要。 _:表示没有外部参数标签。

您应该写

#selector(selectHeaderView(gesture:))

您还可以让Swift决定参数标签

#selector(selectHeaderView)