When should I use anyObject insted of UIButton in swift?

ぃ、小莉子 提交于 2019-12-22 10:25:20

问题


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.


回答1:


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
}



回答2:


The purpose of using an abstract AnyObject type for an IBAction may be advantage for a situation in which you have multiple UI objects that should trigger the same action. An example of this would be if you wanted to have a button and a gesture recognizer share the functionality of a common action. Even with a shared action, it would be possible to have different execution paths for the two objects.



来源:https://stackoverflow.com/questions/34972807/when-should-i-use-anyobject-insted-of-uibutton-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!