Sergey Grishchev
Sergey Grishchev

Reputation: 12051

update UILabel with the content of UICell from a different view

I have an NSArray tablePeople which makes up my UITableView on my 1st View Controller PeopleController. I have a UILabel personsName on my second View Controller PeopleDetailsController which I want to update with the contents of cell.textLabel.text of each row in my TableView. I have this method but it's not working:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    ((PeopleController *)segue.destinationViewController).delegate=self;
    if ([[segue identifier] isEqualToString:@"toPeopleArticle"]) {

        NSIndexPath *indexPath = (NSIndexPath *)sender;

        PeopleDetailsController *mdvc = segue.destinationViewController;
        mdvc.personsName.text = [self.tablePeople objectAtIndex: indexPath.row];
    }
}

I also have this code when the cell is selected:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"toPeopleArticle" sender:indexPath];
}

I have to note that PeopleDetailsController is a modal view and PeopleController is a navigation view controller.

EDIT: The text on the UILabel on the 2nd VC is just not being updated, it stays the same, that's the whole problem.

Upvotes: 0

Views: 97

Answers (1)

Omar Abdelhafith
Omar Abdelhafith

Reputation: 21221

Change the following

PeopleDetailsController *mdvc = segue.destinationViewController;
mdvc.personsName.text = [self.tablePeople objectAtIndex: indexPath.row];

to

PeopleDetailsController *mdvc = segue.destinationViewController;
mdvc.personsNameString = [self.tablePeople objectAtIndex: indexPath.row];

where personsNameString is a property of type NSString in the PeopleDetailsController

Now in PeopleDetailsController viewDidLoad or viewWillAppear Function set the value of the label to the value of the property

mdvc.personsName.text = personsNameString;

Upvotes: 1

Related Questions