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.
问题:
回答1:
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:
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:
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.
回答2:
- Open Assistan Editor (Split View, so that you see Interface Builder on the Left and Your View Controller on the right.)
- Ctrl-Klick & Drag on the button and drag it to the Controller (you'll see a blue line).
- An
IBOutlet
will be generated in your ViewController and you can access it.