Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

ios - Can I use multiple segues with one UITableViewDelegate?

I have a UITableView listing multiple types of objects, and I'd like to segue to a different view depending on which type of object the user selects.

Is it possible to do this by using multiple segues, and, if so, how?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Of course ! Define all your segues on your storyboard, by ctrl-dragging from the tableViewController (not a row, the tableViewController itself) to the next view. Give them IDs, so you know which one to call. When all your segues are defined visually, go to the code. In your tableView's delegate, in didSelectRowAtIndexPath, simply call the segue you want, by checking indexPath.row :

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

    switch (indexPath.row) {
        case 0: [self performSegueWithIdentifier:@"Segue0" sender:self];
                break;
        case 1: [self performSegueWithIdentifier:@"Segue1" sender:self];
                break;
        [...]
        default: break;
    }
}

That way, the segue with the ID "Segue0" will be fired when the user selects the first row, and so on.

You can also add the line : [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] animated:YES]; at the beginning of didSelectRowAtIndexPath so the row does not remain selected after the user touched it !

Edit : This works for both static and dynamic cells ! Be careful to ctrl-drag your segue from the tableViewController, not a cell !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...