1

I have a func below, but can`t send href for func.

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

    return headerView
}

@objc func headerClick(_ sender: AnyObject, href: String) {
    print(href)
}
ctwyw
  • 51
  • 5
  • 1
    you can only get the sender parameter I believe. what are you trying to pass to your handler function? – Christian Abella Jun 26 '18 at 01:58
  • You can't add params through the addTarget. You should implement protocol in order to pass the params. – Nizzam Jun 26 '18 at 02:02
  • @ChristianAbella I am trying to pass diff type for show diff controller. so that this button can responds to different requests – ctwyw Jun 26 '18 at 02:17
  • refer to this for an answer https://stackoverflow.com/questions/24814646/attach-parameter-to-button-addtarget-action-in-swift – TheNitram Jun 26 '18 at 02:17
  • @Nizzam so .. I need to extension my button only ?~ – ctwyw Jun 26 '18 at 02:18

1 Answers1

0

Custom your button:

class LinkButton: UIButton {
  var href: String?
}

in your function:

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)
  }
}
Tien-Nvan
  • 88
  • 2
  • 9
  • 4
    Good answer. Now PLEASE add some explanation as to WHY this is a good answer! We are here to help not *just* the person asking something, we are trying to help others! –  Jun 26 '18 at 03:10