UILabel not updating

后端 未结 5 1185
太阳男子
太阳男子 2021-01-02 10:38

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:

相关标签:
5条回答
  • 2021-01-02 11:10

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

    tmpVC.myLabel.text = tmpObj.myTitle;

    0 讨论(0)
  • 2021-01-02 11:18

    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];
    
    0 讨论(0)
  • 2021-01-02 11:18

    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.

    0 讨论(0)
  • 2021-01-02 11:26

    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.

    0 讨论(0)
  • 2021-01-02 11:27

    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.

    0 讨论(0)
提交回复
热议问题