swift 3.0
Given
1 2 3 4 |
func tap(gestureReconizer: UITapGestureRecognizer) { ... } |
should now be written as
1 |
#selector( tap(gestureReconizer:) ) |
If you were to follow swift 3 guidelines for declaring functions
1 2 3 |
func tap(_ gestureRecognizer: UITapGestureRecognizer) { ... } |
in that case, we’ll have to declare our selector like so:
1 |
#selector(tap(_:)) |
In a Single View App
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let actionOne = #selector(sample(sender:)) let button = UIButton(type: UIButtonType.custom) button.addTarget(self, action: actionOne, for: UIControlEvents.touchUpInside) button.frame = CGRect(x: 80, y: 80, width: 200, height: 80) button.titleLabel?.text = "push me" button.backgroundColor = UIColor.red self.view.addSubview(button) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func sample(sender: UIButton) { print("--------- BUTTON --------") print("sender - \(sender)") print("\(sender.buttonType)") print("\(sender.titleLabel!.text!)") print("\(sender.frame)") print("-------------------------") } } |
If you want to pass in additional information into the sample function, you can over-ride UIButton with your custom button class and set a property there.
Then, when you get your button object in sample function, just access that property.