Reputation: 33
MY PROBLEM: when the user a certain cell to select a player, I want the rest of the information related to that player to show up on the new view controller
So far my method looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
IPlayerViewController *detailViewController = [[IPlayerViewController alloc] initWithNibName:@"IPlayerViewController.h" bundle:nil];
Player *playerF = [playerArray objectAtIndex:indexPath.row];
detailViewController.PlayerLabel.text=playerF.Name;
[self.navigationController pushViewController:detailViewController animated:YES];
}
However the PlayerLabel that i created in IPlayerViewController (and connected the UILabel to the label in storyboard) is blank.
If this looks familiar it is because i am following the following tutorial, but trying to add my own things. Thanks a lot! http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
EDIT: I am USING storyboard. However, in the predefined method, xcode came with the following comment to initialize with Nib Name which threw me off.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
So it seems like I should use prepareForSegue. Is this something I call in the didSelectRowAtIndexPath or do i ignore that method?
Upvotes: 3
Views: 5901
Reputation: 42598
If you are using storyboards, then most likely you won't see a call to -tableView:didSelectRowAtIndexPath:
. If you are using segues to get to the next view, then you will need to use -prepareForSegue:sender:
see: How to pass prepareForSegue: an object.
Update
Do you have a segue from the table view cell to your detail view controller?
If you have a segue, then -tableView:didSelectRowAtIndexPath:
is not what you want. If you have a segue, create a -prepareForSegue:sender:
method in your table view controller. It will look something like:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
Player *player = [self.playerArray objectAtIndex:indexPath.row];
IPlayerViewController *destination = segue.destinationViewController;
destination.player = player;
}
Assuming of course that IPlayerViewController has a player property.
Hope that helps.
Upvotes: 4
Reputation: 31026
Since you're initing with a nib, I'll assume you're not using a storyboard....
I think you're doing everything right except, don't try to put the name directly into the label because it hasn't loaded yet. Create a property in your detail controller that's just a string to hold the name. Set that in your code for didSelectRowAtIndexPath:
and then update the label in viewDidLoad:
in the detail controller.
Upvotes: 0
Reputation: 359
In your next view controller.h you need a player property.
@property (nonatomic,strong) Player *player;
If you are using storyboards, you will have to save the selectedRow index in your class, and implement the prepareForSegue method, like:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NextViewController *controller = segue.destinationViewController
controller.player = [self.players objectAtIndex: YOUR_SAVED_INDEX];
}
Understood the concept?
Upvotes: 0