Search code examples
iosswiftuibuttonibactionanyobject

When should I use anyObject insted of UIButton in swift?


When should I use anyObject insted of UIButton in swift? I am making an IBAction for my button that will be used to do more than on task on of the tasks is to switch to the next view.


Solution

  • Ultimately, it really doesn't matter.

    You can choose to use the parameter of (sender: AnyObject) or you can use (sender: UIButton).

    There might be times however where you might have to cast AnyObject as a UIButton if you need access to the properties provided by UIButton.

    For example let's say you have and you want the button to disappear after it is clicked.

    func doSomething(sender: AnyObject) {
        let button: UIButton = sender as! UIButton
        button.hidden = true
    }