Objective-c – Updating UI Elements with Controller Constructor

objective-c, xcode

I have an NSSlider (slider) and an NSLabel (label) on a window. I also have a class "Controller" that updates the label whenever the slider's value is changed.

The default position of the slider is 0.5, I'm trying to get is where Controller's constructor updates the label upon program launch.

The following is the implementation file for my attempt to do this. Everything works fine except the label is always 0 when I start the program.

@implementation Controller{}-(id)init{    NSLog(@"initializing...");      [self updateLabel];         return self;}- (IBAction)sliderChanged:(id)sender{    [self updateLabel];   }- (void)updateLabel{    label.text = [NSString stringWithFormat:@"%.1f", slider.value];}@end

In the console I see the text "initializing…", but the label is never updated. What am I missing?

Best Solution

The controller may be getting initialized (where is your call to [super init]?), but that doesn't mean the outlets are hooked up. The proper way to do that would be to rely on a viewDidLoad, windowDidLoad, or awakeFromNib method.