Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yeah, this in one of the key questions that I had myself before spending hours in tutorials. I dont't think this is well documented for experienced programmers that just dive into app development. </p> <p>The usual way is that you declare a property of the type of the object (ie. <code>UIButton</code>) within the .h file of the related view controller class (A <code>UIViewController</code> subclass or <code>UITableViewController</code> respectively). It is key to add the keyword <code>IBOutlet</code> to it. </p> <p>The keyword enables Interface Builder (IB; now your storyboard editor) to identify those properties that are supposed to be linked to UI items that are created and maintained within IB. You link this reference outlet by dragging a line from the connections inspector (right hand pane in IB view, right most icon on the tab bar of the pane, the one with the arrow in the circle) to the actual object either in the preview (center pane) or in the view hierarchy view. </p> <p>Within the .h file (and within the .m file if you synthesize it there) your connection is acknowledged by a dot in the circle left of the declaration. It is an empty circle when the property is not connected. There is no circle at all when the property is not marked as <code>IBOutlet</code>. </p> <p>Now, when you access the property from within (or without - possible but not recommended) the view controller, it should be different from nil in most methods (viewDidLoad and follwonig methods). You can get or set values and by setting them you will actually change the related UI item on the screen.</p> <p>A lot of UI items are controls (subclasses of UIControl, UIButton for instance) and have actions assigned. By calling an action the control sends the message to its controller, that the user did something of interest. The user may have changed the value of a UISwitch or pressed the UIButton. That is pretty similar to properties. Declare a method. No need to doing so in the .h any more but I still follow that pattern. Start typing <code>- (IBAction) someName</code> and the autocompletion will guide you through. You can see <code>(IBAction)</code> as a synonyme for <code>(void)</code>, but it tells the xcode IDE that this is supposed to be linked to a control as action. IBActions receive one parameter of type (id) or (UIView) or (UIControl). (I am not quite sure about the actual type). Key is that it receives a reference to the very control instance on which the action was performed - the sender. Typical pattern: </p> <pre><code>-(IBAction) myButtonWasPressed:(UIButton *)sender; </code></pre> <p>You can write books about whether it is wise to typcast it to UIButton at this stage or not. However, it works but you should test within the method and make sure that it really is a <code>UIButton</code> (eg. by using <code>isKindOfClass:[UIButton class]</code>) before you access any property or method that is unique to UIButtons. If you just react on the IBAction beeing invoked and don't acces the sender at all, then you may ignore the sender or even omit the parameter. I think the following should work too: -(IBAction) myButtonWasPressed;</p> <p>Of course you will need to implement the method in the .m file. </p> <p>You link this action to the control in a similar way as the property by dragging a line within IB. In the case of a button you will be asked to assign this to an event. For buttons the <code>touchUpInside</code> event is not a bad choice. You could assin the same action for a number of events if you find that you have reasons for doing so. (Can't think of any reason, but it works nicely). </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload