Swift:如何在button.addTarget中添加参数

时间:2018-06-26 01:53:29

标签: swift selector addtarget

我下面有一个函子,但无法发送href函子。

func someFunc() {
    linkBtn.addTarget(self, action: #selector(headerClick(_:href:)), for: .touchUpInside)

    return headerView
}

@objc func headerClick(_ sender: AnyObject, href: String) {
    print(href)
}

1 个答案:

答案 0 :(得分:0)

自定义按钮:

class LinkButton: UIButton {
  var href: String?
}

在您的功能中

func someFunc() {
  linkBtn.href = yourHref
  linkBtn.addTarget(self, action: #selector(handleClink(_:)), for: .touchUpInside)
}
@objc func handleClick(_ sender: LinkButton) {
  if let href = sender.href{
    print(href)
  }
}