troncat
troncat

Reputation: 41

iOS 5 - Segue from one TableViewController to another TableViewController

I'm trying to select a detail disclosure button (or a row) from one TableViewController and segue to another TableViewController, without passing any data from the first to the second TableViewController. So pretty much the first TableViewController acts as a table that will navigate to different TableViewControllers depending on the detail disclosure button (or row) selected.

So far my code for doing this looks like the following:

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


    NSUInteger rowNumber = [indexPath row];   
    NSString *categoryCode = [self.categoryCodes objectAtIndex:rowNumber];

    self.categoryData = [self.commuterDict objectForKey:categoryCode];

    [self performSegueWithIdentifier:@"ShowPlaces" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowPlaces"]) {

        PlacesViewController *placesViewController = [segue destinationViewController];

        // --> I believe I'm missing something here to access a specific row? <--



    }
}

I hope someone can help me!

Upvotes: 4

Views: 5373

Answers (5)

Eric
Eric

Reputation: 7877

The sender you use in prepare for segue can be whatever you want. If you want to know the row, send the row, or the index path:

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


    NSUInteger rowNumber = [indexPath row];   
    NSString *categoryCode = [self.categoryCodes objectAtIndex:rowNumber];

    self.categoryData = [self.commuterDict objectForKey:categoryCode];

    [self performSegueWithIdentifier:@"ShowPlaces" sender:indexPath];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowPlaces"]) {

        PlacesViewController *placesViewController = [segue destinationViewController];

        // Row information is supplied as the sender
        NSIndexPath *indexPath = sender;


    }
}

If you want to use the actual row, then make that the sender

[self performSegueWithIdentifier:@"ShowPlaces" sender:[tableView cellForRowAtIndexPath:indexPath]];

Upvotes: 0

Tr0ttr
Tr0ttr

Reputation: 1

If your sender object is a tableviewcell, the following code snippet should work:

NSIndexPath *indexPath = [[[segue sourceViewController] tableView] indexPathForCell:sender];

Otherwise try

NSIndexPath *indexPath = nil;
if ([sender isMemberOfClass:[UITableViewCell class]]) 
{
    indexPath = [[[segue sourceViewController] tableView] indexPathForCell:sender]; 
}
else if ([[sender superview] isMemberOfClass:[UITableViewCell class]])
{
    indexPath = [[[segue sourceViewController] tableView] indexPathForCell:(UITableViewCell *)[sender superview]];
}
else if ([[[sender superview] superview] isMemberOfClass:[UITableViewCell class]])
{
    indexPath = [[[segue sourceViewController] tableView] indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
}

Upvotes: 0

Caleb
Caleb

Reputation: 124997

The documentation for UIViewController's -prepareForSegue:sender: says:

Because segues can be triggered from multiple sources, you can use the information in the segue and sender parameters to define the context for the segue and pass the appropriate information. For example, if the segue originated from a table view, the sender parameter would identify the table view cell that the user tapped. You could use that information to set the data on the destination view controller.

Given that, I'd look first at the sender parameter and expect it to be a pointer to the table cell. If you know which table the cell should be in, you can use -indexPathForCell: to retrieve the cell's index path.

Upvotes: 2

Martin
Martin

Reputation: 800

you can drag segue from the view controller to the view you want to get shown when pressing the detail disclosure button. you can fire the segue from within your code as follows

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

Cheers from Austria Martin

Upvotes: 0

cc.
cc.

Reputation: 3051

You get the specific row from the indexPath in the tableView:accessoryButtonTappedForRowWithIndexPath: function. Specifically, the row that was tapped was indexPath.row. (If your table has multiple sections, then you can read the section out of indexPath, too, but it sounds like you're just using a simple table.)

You can store the current row in a variable and then read it in prepareForSegue:sender to choose where to go.

Upvotes: 2

Related Questions