UILabel text does not change, however xx.title works

I have two view controllers. In the controller of the first view, I have a list of names. When I click on this, I want the same name to appear in the second view controller.

I have the code below.

-(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 @property (retain, nonatomic) IBOutlet UILabel *detailLabel;

.

What I was expecting is when I click " Name 1

" I will see Name 1

both text in the UILabel and on the UINavigationBar. However, I only see Name 1

on the navigation bar, not on the UILabel.

+3


source to share


2 answers


It is not recommended to access the UIView element at this point in the program flow. Setting the prop.detailLabel.text value may not load the view. When the view is loaded later, the UIView is updated with the default settings specified in the XIB file in the IB.

You have to set the NSString property, say

@property (retain, nonatomic) NSString *propName;

      

assign it before pushing the view controller just like you did. But use this property, not UILable. And in PropDetailViewController in viewDidLoad do the following:



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

  self.detailLabel.text = propName;

} 

      

You can use viewWillAppear instead of viewDidLoad. Because viewDidLoad COULD is executed when you assign property value.

If you want to be on the persistence side, come up with a new init method in which you pass in all the values ​​that you want to configure beforehand. But I've never done this in conjunction with a storyboard (where you can use an instance ... rather than init ...) and so I can't give any advice out 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 (perhaps _propName if you autosynthesize) AND set the UILable text plus setting the UILable text to viewDidLoad.

+4


source


Try this: in .h

@property (retain, nonatomic) NSString *detailText;

      

in PropDetailViewController.m

Change the line of code with



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

      

in ViewDidLoad:

[self.detailLabel setText:self.detailText];

      

+1


source







All Articles