I appreciate everyone who says usage tags, but actually you need to extend the UIButton class and just add an object there.
Tags are a hopeless way. Extend UIButton like this (in Swift 4)
import UIKit class PassableUIButton: UIButton{ var params: Dictionary<String, Any> override init(frame: CGRect) { self.params = [:] super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { self.params = [:] super.init(coder: aDecoder) } }
then your call could be Selector(("webButtonTouched:")) (NOTE: A colon ":" in Selector(("webButtonTouched:")) )
let webButton = PassableUIButton(frame: CGRect(x:310, y:40, width:40, height:40)) webButton.setTitle("Visit",for: .normal) webButton.addTarget(self, action: #selector(YourViewController.webButtonTouched(_:)), for:.touchUpInside) webButton.params["myvalue"] = "bob"
then finally catch it all here
@IBAction func webButtonTouched(_ sender: PassableUIButton) { print(sender.params["myvalue"] ?? "") }
You do this once and use it throughout your project (you can even force the child class to have a common “object” and put whatever you like into the button!). Or use the above example to add an inexhaustible number of key / string parameters to the button. Really useful for including things like URLs, confirmation of message methodology, etc.
On the side, it is important that the SO community realizes this, there is a whole generation of bad practice that is being reduced on the Internet by an alarming number of programmers who do not understand / learn / miss the point of the object extensions concept
Mr Heelis Oct. 25 '17 at 13:55 on 2017-10-25 13:55
source share