AnyObject 和 UIbutton 作为发送者有什么区别?

编程入门 行业动态 更新时间:2024-10-11 01:17:20
本文介绍了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 作为发送者有什么区别?

本文发布于:2023-11-25 20:16:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   AnyObject   UIbutton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!