Old McStopher
Old McStopher

Reputation: 6349

How to access attributes for a UI element in Interface Builder

For example, I have a UISlider that calls - (IBAction)sliderMoved:(id)sender, which performs some task once a new value has been "slid" to through the Value Changed event. But what if I want to do something based on whatever its initial value is?

Hard-coding something to match the default value in Interface Builder would be sloppy and hard to debug later if someone changes the nib file. So, how can I access the default value set for this UISlider in Interface Builder?

AND, more generally speaking, how does one access these attributes for any such UI element? Are there setter/getter methods already synthesized for these?

Upvotes: 0

Views: 398

Answers (2)

phaxian
phaxian

Reputation: 1078

As Maunil said, Use bindings to connect the UISlider in InterfaceBuilder to a UISlider object in the code. Once this is done, you can access all its methods & properties.

To set the initial value, use the setValue: property of the UISlider, something like:

- (void)viewDidLoad {
    ...
    // where mySlider is declared as a UISlider object and bound to your XIB slider
    [mySlider setValue:some_default_value];
    ...
}

See the UISlider Class Reference documentation for more information.

Upvotes: 1

mets19
mets19

Reputation: 88

You can declare the slider in the header file as IBOutlet, and then hook it up in IB. Then you will be able to access all its properties in code.

Upvotes: 1

Related Questions