I have a UIViewController that needs to set a few labels when it receives a new (object) value for one of its properties:
-(void)setCurrentEvent:(Event *)e {
[currentEvent release];
currentEvent = [e retain];
self.dateLabel.text = currentEvent.subtitle;
self.summaryTextView.text = currentEvent.summary;
self.avgRatingLabel.text = [NSString stringWithFormat:@"%.1f",currentEvent.avgRating];
[self setTitle:currentEvent.title];
[self.view setNeedsDisplay];
}
I found that when the values are set for the first time, the label and text view objects are not initialized yet and thus their new values are not set. After the initial call of setCurrentEvent all goes well, but I think I am relying on lazy loading a bit too much here?
Assuming it is being awaken from a nib or loaded from one:
Until your view controller’s
viewDidLoadmethod is called, there is no guarantee any of your IBOutlets are set.What you are seeing I suspect is that the first time the values are set your view hasn’t loaded yet, and by the time the values are set for the second, they have.
You should defer processing until the view is loaded, or create the views yourself as required.
EDIT
In light of your comment, it seems you are loading from a nib. In this case I would perhaps retain the instance variable (
currentEventsay), then call a method likeupdateUIwhich sets the properties correctly. I would then also putupdateUIin viewDidLoad so when the labels become available they are updated against the current event.