What's the difference between AnyObject and UIbutton as sender?

前端 未结 3 517
南笙
南笙 2020-12-29 09:48

When defining an IBAction, there\'s an option for Anyobject and UIButton, both works, what\'s the difference?

3条回答
  •  春和景丽
    2020-12-29 10:09

    If it's anyObject, then it doesn't have any of UIButton's properties. When you click the button and the IBAction fires, sender contains information about the thing that triggered the action.

    For example, for a UIButton, you might want to query the UIButton's text when the IBAction triggers.

    However, in a situation where the IBAction is connected to two different UI controls, let's say, a button and a slider, querying the sender when it's of type UIButton (while the triggering UI element is the UISlider) will crash the program. If you have AnyObject, you'll be able to test if the sender is a UIButton or a UISlider, and then do something.

    In general, if you don't care about sender, leave it blank, so people reading your code will know that you aren't using sender for anything.

提交回复
热议问题