简体   繁体   中英

Updating Label of a View Controller by a NSDictionary

I am new in Xcode5 and IOS7 and I am having problems updating the text of a couple of labels and an image. I have a NavigationController -> InitialViewController ( UITableView with custom Rows) and when you click in a Row I want to show a UIViewController with the information of the previous RowCell , the information is stored in a NSMutableArray filled with NSDictionary with keys(name, team, image). The problem is that the View is not updating the text of the labels and the image, I can't find what is wrong in the code below. I have debugged that it is working until the pushView , once there the information inside the label and the image is not getting updated.

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

    NSDictionary *playersVC = [players objectAtIndex:indexPath.row];

    ProfilePlayerViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"ProfilePlayerViewController"];

    [self.navigationController pushViewController:profile animated:YES];
    [profile.playerName setText:[playersVC objectForKey:@"name"]];
    [profile.teamName setText:[playersVC objectForKey:@"team"]];
    [profile.playerImage setImage:[UIImage imageNamed:[playersVC objectForKey:@"image"]]];
}

The rest of configuration it seems to be right, I am just following part of a tutorial with IOS5 and it seems to work for them.

It will not work this way. You have to create a @property NSDictionary in your ProfilePlayerViewController. Then when you click on a row at your first View Controller, assign that NSDictionary to that property like this.

NSDictionary *playersVC = [players objectAtIndex:indexPath.row];
ProfilePlayerViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"ProfilePlayerViewController"];
profile.myDictionary = playersVC ;

Now at ViewDidLoad method of your ProfilePlayerViewController , update the labels and images. It will certainly work. Hope it helps.

Try to create instance variables in your ProfilePlayerViewController like

@property (nonatomic,strong) NSString *playerName;
@property (nonatomic,strong) NSString *team;
@property (nonatomic,strong) NSString *imageName;

After you instantiate this ViewController , set these properties from the dictionary, like so:

ProfilePlayerViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"ProfilePlayerViewController"];

[self.navigationController pushViewController:profile animated:YES];
profile.playerName =  [playersVC objectForKey:@"name"];
profile.team = [playersVC objectForKey:@"team"];
profile.imageName = [playersVC objectForKey:@"image"];

And in your [self viewDidLoad] method, set your UI Elements.

-(void)viewDidLoad {
      [super viewDidLoad];
      self.playerNameLabel.text = self.playerName;
      self.teamLabel.text = self.team;
      [self.playerImage setImage:[UIImage imageNamed:self.imagName]];
}

This should solve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM