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

查看:15
本文介绍了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,您可以发送自我".这还可以让您区分刷新是通过操作还是以编程方式完成的.

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 属性.只需键入 cast it 即可使用它们.

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

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

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