user1904273
user1904273

Reputation: 4764

IOS/Xcode/Storyboard: How can I segue from table row to modal view controller

This is a storyboard question, I think.

How do you create a segue from a table row to a new view controller?

Here is a picture of what it is supposed to look like.

storyboard screenshot

I tried control dragging from the table to the view controller on the bottom but it does not take. If I embed the view controller in a navigator I can create a push segue but it does not work when you click on it.

This is the code I am using for that but first I would like to be able to replicate the storyboard in the picture. In any case I want the new view controller to pop up when you click on the table row.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // Store Selection
    [self setSelection:indexPath];
}


...

else if ([segue.identifier isEqualToString:@"updateToDoViewController"]) {
            NSLog(@"segue to update");

            // Obtain Reference to View Controller
            UpdateTodoVC *vc = (UpdateTodoVC *)[segue destinationViewController];

            // Configure View Controller
            [vc setManagedObjectContext:self.managedObjectContext];

            if (self.selection) {
                // Fetch Record
                NSManagedObject *record = [self.fetchedResultsController objectAtIndexPath:self.selection];

                if (record) {
                    [vc setRecord:record];
                }

                // Reset Selection
                [self setSelection:nil];
            }
        }
    }

Thanks for any suggestions.

Upvotes: 2

Views: 911

Answers (1)

Armin
Armin

Reputation: 1150

You can't ctrl+drag from a "Prototype" cell! You can only do that with static cells. So you have to ctrl+drag from the UITableViewController to the next view controller, give it an identifier of updateToDoViewController, and perform the segue programatically:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // Store Selection
    [self setSelection:indexPath];

    // Perform the segue with identifier: updateToDoViewController
    [self performSegueWithIdentifier:@"updateToDoViewController" sender:self];
}

Upvotes: 5

Related Questions