简体   繁体   中英

Change label text depending on what Table View Cell was pressed (Storyboard)

I have some cells in a table view that when pressed leads to a view controller that has a label that I want to change.

Example: I have cells representing the biggest citys in America, I press Los Angeles and I get sent to the view controller with the label that changes to a number representing LA. If I go back and press New York the label now displays a number representing New York.

I would guess I give the label and cells identifiers and make some kind of if/else if.

Something like this:

"if 'newyorkcell' isPressed setText 'numbercell' = "12345";"

But with real code.

Thanks in advance!

For this you'll have to make a public outlet of your label in your view controllers header file:

@interface YourViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *yourLabel;

@end

Then you can set that labels text via didSelectRow, for this you have to give your detail view controller a storyboard id (in the storyboard) to be able to access it here in code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];

    detailViewController.yourLabel.text = [self.yourArray objectAtIndex:indexPath.row];

    [self.navigationController presentViewController:detailViewController animated:YES completion:nil];
}

Instead of using if/elses you could use an array or something where you have all your data in and access it using its index.

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