UILabel text not changing, however xx.title is working

浪子不回头ぞ 提交于 2019-12-12 12:29:19

问题


I have two view controller. In first view controller I have list of names. When I click on that, I want the same name to be displayed in second view controller.

I have below code.

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // PropDetailViewController is second view controller
    PropDetailViewController *prop = [self.storyboard instantiateViewControllerWithIdentifier:@"prop"];

    ListOfProperty *propList = [propListFinal objectAtIndex:indexPath.row];

    NSString *myText = [NSString stringWithFormat:@"%@", propList.addressOfFlat];
    prop.detailLabel.text = myText;
    prop.title  = myText;

    [self.navigationController pushViewController:prop animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

and in PropDetailViewController, I have @property (retain, nonatomic) IBOutlet UILabel *detailLabel;.

What I was expecting is when I click say Name 1, I will see Name 1 as text in UILabel and on the UINavigationBar too. However I only see Name 1 on navigation bar and not on UILabel.


回答1:


It is not advisable to access an UIView item at that point in the program flow. When setting the value of prop.detailLabel.text the view may not have been loaded. When the view is loaded later then the UIView is updated with the default settings given in the XIB file in IB.

You should rather set an NSString property, lets say

@property (retain, nonatomic) NSString *propName;

assign it before pushing the view controller as you do. But use this property and not the UILable. And in PropDetailViewController in viewDidLoad do the following:

(void) viewDidLoad {
  // call super viewDidLoad and all the works ...

  self.detailLabel.text = propName;

} 

Instead of viewDidLoad you could use viewWillAppear. Because viewDidLoad COULD be executed already when you assign the property's value.

If you want to be on the save side then invent a new init method where you hand over all the values that you want to be set upfront. But I never did that in combination with storyboard (where you may use instantiate... rather than init...) and therefore I cannot give any advise out of the top of my head.

Another clean approach would be to stick with the propName property but to implement a custom setter -(void)setPropName:(NSString)propName; where you set the property (probably _propName if you autosynthesize) AND set the UILable text plus setting the UILable text within viewDidLoad.




回答2:


Try this: in .h

@property (retain, nonatomic) NSString *detailText;

in PropDetailViewController.m

Change line of code with

 NSString *myText = [NSString stringWithFormat:@"%@", propList.addressOfFlat];
    prop.detailText = myText;
    prop.title  = myText;

in ViewDidLoad:

[self.detailLabel setText:self.detailText];


来源:https://stackoverflow.com/questions/14435261/uilabel-text-not-changing-however-xx-title-is-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!