views:

314

answers:

6

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

+2  A: 

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.

David Gelhar
The connection exists: the file owner outlets shows 'my Label' - 'Label (myLabel)'.
iFloh
+1  A: 

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.

Codezy
hmm, I think you're right there, but (a) when I try to add the ".text" I get a compiler error "member 'myLabel' not in structure or union"<br>(b) the preceeding setText statement does not produce any updates in the view.<br>Maybe (a) leads to the view not properly connected to the nib or controller, but can't see where the break could sit ....
iFloh
added more to my answer, see above
Codezy
Hi Codezy, sorry, that was a typo (corrected above). IB woudn't let me connect the 2 w/out a matching class. The Definition is UILabel ...
iFloh
Any compiler warnings?
Codezy
no, compiles without error or warning
iFloh
Are you releasing that label anywhere other than the dealloc, because that could do it.
Codezy
Not a dealloc, but have a look at my updates in the question above (under THERE'S THE LIGHT). it seems the code sequence needed changing
iFloh
A: 

It may be due to wrong Data Type assign to tmpVC.myLabel.text in NSLog() statement. Try this NSLog(@"%s", tmpVC.myLabel.text); it may worked.

RRB
%@ on a UILabel type would sooner cause a crash than just printing (null) assuming it was not actually null. Would it not?
Jasconius
Hi Rajendra, just tried that based on Codezy's response, but no that leads to a compiler error (event when declaring the NSLog with a %s ...
iFloh
A: 

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

tmpVC.myLabel.text = tmpObj.myTitle;

Jeff
+1  A: 

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.

Rengers
+1  A: 

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];
Hwee-Boon Yar
thanks Yar, makes perfect sense ! Confusion gone
iFloh