AnyObject和UIbutton作为发件人有什么区别? [英] What's the difference between AnyObject and UIbutton as sender?

查看:156
本文介绍了AnyObject和UIbutton作为发件人有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在定义IBAction时,Anyobject和UIButton都有一个选项,两者都有效,有什么区别?

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

推荐答案

是的,两者都有效。不同之处在于,通过将其声明为按钮,您可以获得一个类型为UIButton的引用而不是AnyObject(或Objective-C中的id)。如果你不这样做,你将不得不在代码中手动完成。

Yes, both works. The difference is just that by declaring it to a button, you get a typecasted reference as UIButton instead of AnyObject (or id in Objective-C). If you did not do that you will have to do it manually inside the code.

你应该更喜欢把它留给AnyObject,以防你的行为有你想要的代码从其他任何地方调用,而不仅仅是按钮操作。

You should prefer leaving it to AnyObject in case the action has some code you would like to call from anywhere else, rather than just the button action.

例如刷新按钮,您可能需要以编程方式进行刷新。如果你已经将你的动作参数设置为UIButton,你必须发送一个UIButton(但为什么???你是以编程方式进行的,对吧?)。如果它是AnyObject,您可以发送'self'。这也将使您区分刷新是在动作上还是以编程方式完成。

For example a refresh button, you might have to do a refresh programatically. If you have set your action parameter to UIButton, you have to send an UIButton (but why??? You are doing it programatically, right?). Whereas if it is AnyObject, you can send 'self'. Which will also make you distinguish whether the refresh was done on action or programatically.

希望解释。
很抱歉没有代码。我不是很喜欢Swift编码。欢迎编辑。

Hope that explains. Sorry for no code. I am not into Swift coding much. Edits are welcome.

编辑

所以,即使它是AnyObject,它确实有你的UIButton属性。只需输入它就可以使用它们。

So, even if it is AnyObject, it does have your UIButton properties. Just type cast it to use them.

这篇关于AnyObject和UIbutton作为发件人有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆