我怎样才能获得用故事板创建的按钮的对象 [英] how can i get the object of a button created with storyboard

查看:26
本文介绍了我怎样才能获得用故事板创建的按钮的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过拖动故事板中的按钮图标在视图控制器上创建了一个按钮.我认为这会生成一个对象.有没有一种方法可以获取 id,即那个按钮的对象?是否有一种方法可以为该按钮指定标签或标识符,并且我们可以使用某些方法来引用它?提前感谢您的回答.

I created a button on a view controller by dragging the icon of button in storyboard. I thought this would generate an object. is there an approach that I can get the id, i.e. the object of that button? Is there a method that we designate a label or identifier for that button and we can refer to it with certain methods? Thanks in advance for answering.

推荐答案

当您将一个按钮从对象库拖到故事板的场景中时,当该场景在运行时实例化时,将创建与该按钮关联的对象.如果您想引用该对象以便以编程方式与其交互,则可以打开辅助编辑器:

When you drag a button from the object library onto your storyboard's scene, when that scene is instantiated at runtime, the object associated for that button will be created. If you want to have a reference to that object so you can interact with it programmatically, you can open the assistant editor:

如果您告诉助理编辑器自动"选择正确的关联 .h 文件,您将需要确保为您的视图控制器设置自定义类并简化您的生活:

You will want to make sure you set the custom class for your view controller and you simplify your life a little if you tell your assistant editor to "automatically" select choose the right associated .h file:

然后您可以控制从按钮向下拖动(或右键单击拖动)到助理编辑器窗口并创建一个插座(一个IBOutlet)或一个动作(IBAction):

You can then control-drag (or right-click-drag) from the button down to assistant editor window and create either an outlet (an IBOutlet) or an action (an IBAction):

通过这样做,您就可以引用在场景实例化时创建的按钮对象.

By doing that, you then have a reference to the button object that is created when the scene is instantiated.

这篇关于我怎样才能获得用故事板创建的按钮的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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