Search code examples
objective-ccocoanstextfield

why setStringValue call works in viewDidLoad and viewDidAppear but not in other methods?


I am trying to change the value of the label here.

Can anyone explain me why the following method call:

 [_detailLabel setStringValue:@"this is new label value"];

works if I make the call from viewDidLoad and viewDidAppear methods, but it doesn't work in the method that I created:

-(void)changeLabelValue : (NSString *) newVal {
    [_detailLabel setStringValue:@"this is new label value"];
    NSLog (@"Hello from changeLabelValue method");
}

Please note that when I call this method from anywhere from my code, the NSLog message IS displayed but the value of the label is not changed...

Any help is deeply appreciated.

Pointing me to resource where I can learn more on this subject will also do the trick, and will be also deeply appreciated.

Regards, John.


Solution

  • Make sure that changeLabelValue: is being called after viewDidLoad is called (or the view controller has been displayed). If you call it before the view is loaded, _detailLabel will be nil since it hasn't been loaded yet.