user306766
user306766

Reputation:

UILabel not updating

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:

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 ???

Upvotes: 9

Views: 7141

Answers (5)

Codezy
Codezy

Reputation: 5570

Is it not because your NSLog is trying to print out the actual label object. Should you not have

NSLog(@"%@", tmpVC.myLabel.text);

In Response to the added information: Your other issue would appear to be that you have linked an NSString to your label. You have to link it to a UILabel. So where you declare your myLabel var, change it to UILabel *myLabel, and the same for any matching property.

Upvotes: 0

Jeff
Jeff

Reputation: 2838

Did you @synthesize your myLabel property in myViewController.m? You should be able to do something like:

tmpVC.myLabel.text = tmpObj.myTitle;

Upvotes: 1

Hwee-Boon Yar
Hwee-Boon Yar

Reputation: 512

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];

Upvotes: 13

Rengers
Rengers

Reputation: 15228

Not sure but I think this is what's going on:

When the view is pushed, the controller loads the view from the nib and hooks up the actions and outlets. Before this, the outlets are not connected, so tmpVC.myLabel is nil.

If you want to be sure, you could put a breakpoint in viewDidLoad of tmpVC to see when the view is loaded.

Upvotes: 0

David Gelhar
David Gelhar

Reputation: 27900

If tmpVC.myLabel is NULL, that probably indicates that you have not made the necessary connection in Interface Builder from the UILabel to your myLabel instance variable.

Upvotes: 1

Related Questions