UILabel not updating

iphone, objective-c

Solution

That's because the controller's view is lazily created only when accessed. Pushing the controller accesses the view.

Alternatively, if you add a line to access the view property, it will work too:

  myViewController *tmpVC = [[myViewController alloc] initWithNibName:@"NIBfile" bundle:nil];
  tmpVC.view;   // Force view creation
  [tmpVC.myLabel setText:tmpObj.myTitle];   // The debugger shows the text: myTitle = "myText"
  NSLog(@"%@", tmpVC.myLabel);              // NSLog will display "myText"
  [self.navigationController pushViewController:tmpVC animated:YES];

Problem

Sorry the basic question, but this bugs me for a while now. I create a details view from a UITable and try to dynamically set its labels, but they are not updating: ``` - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { myObject *tmpObj = [[myObject objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; myViewController *tmpVC = [[myViewController alloc] initWithNibName:@"NIBfile" bundle:nil]; [tmpVC.myLabel setText:tmpObj.myTitle]; // The debugger shows the text: myTitle = "myText" NSLog(@"%@", tmpVC.myLabel); // NSLog SHOWS NULL [self.navigationController pushViewController:tmpVC animated:YES]; [tmpObj release]; } ``` the connections in Interface Builder are set. The Connections Tab for File Owner shows ``` 'myLabel' - 'Label (myLabel)' ``` any ideas why the value is not coming through? A few more Observations: - I also have an IBAction connected. This method is properly called when I click the connected button. - I got a few pointers to my NSLog-statement, whether that should not better use tmpVC.myLabel.text, but trying also returns NULL. - myLabel is declared as IBOutlet UILabel *myLabel in the interface. The property is defined as nonatomic, retain. THERE'S THE LIGHT: After playing around with it for a bit more I moved the pushViewController statement above the label updates. That resolved the label updates. Working code looks like this: ``` - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { myObject *tmpObj = [[myObject objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; myViewController *tmpVC = [[myViewController alloc] initWithNibName:@"NIBfile" bundle:nil]; [self.navigationController pushViewController:tmpVC animated:YES]; [tmpVC.myLabel setText:tmpObj.myTitle]; // The debugger shows the text: myTitle = "myText" NSLog(@"%@", tmpVC.myLabel); // NSLog SHOWS NULL [tmpObj release]; } ``` But I don't understand why I need to push my viewController first ???

Original source