views:

3395

answers:

3

How does one bind an iPhone SDK control (say a UISlider) using Interface Builder?

Unlike regular Cocoa applications, the bindings tab does not seem to be present when the iPhone-app XIB is opened using Interface Builder

+16  A: 

Cocoa bindings is not available in the iPhone SDK. You have to do everything "by hand" much as you used to have to do on the Mac.

Stephen Darlington
+4  A: 

Declare the control as an IBOutlet in your viewcontroller .h file. In IB, set the File's Owner for the view as your viewcontroller class (Select File's owner, in Inspector window click on the (i) button and set the class as your viewcontroller) Now with the File's Owner selected, click on the blue button with a white arrow in the Inspector window. Bind the IBOutlet for your UISlider to the UISlider on your view.

To get the value of the slider when it changes, create a method in the viewcontroller with the return type IBAction. In the inspector window, if you connect this to the UISlider, it gives you multiple options to select from. Select the "Value changed" option. Now every time the value of the slider is changed your IBAction method will be called.

Hope that helps.

lostInTransit
This is not an answer to the question asked, which was basically how to bind controls to directly affect variables (such as an int or string) with zero intervening code from the programmer.
Kendall Helmstetter Gelner
AFAIK that is not possible using the iPhone SDK so gave the OP an option to get the value of the slider.
lostInTransit
+2  A: 

The iPhone's lack of cocoa bindings support is documented in Apple's dev centre. This is a useful page in general to read if you're a Cocoa developer starting iPhone development.

Key Value Observation (KVO) is still fully supported, however, but you'll need to do the parts you would have done with bindings manually. This makes presenting sorted, dynamic lists of items in, for example, UITableView's much more painful than on Mac OS X where you would just hook in an NSArrayController. See also this question on SO.

Matthew Phillips