how can i get the object of a button created with

2019-05-25 19:42发布

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.

2条回答
Animai°情兽
2楼-- · 2019-05-25 20:24

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:

assistant editor

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:

set class and use automatic mode

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):

outlet

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

查看更多
聊天终结者
3楼-- · 2019-05-25 20:24
  1. Open Assistan Editor (Split View, so that you see Interface Builder on the Left and Your View Controller on the right.)
  2. Ctrl-Klick & Drag on the button and drag it to the Controller (you'll see a blue line).
  3. An IBOutlet will be generated in your ViewController and you can access it.
查看更多
登录 后发表回答