我可以在一个 UITableViewDelegate 中使用多个 segue 吗? [英] Can I use multiple segues with one UITableViewDelegate?

查看:32
本文介绍了我可以在一个 UITableViewDelegate 中使用多个 segue 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView 列出多种类型的对象,我想根据用户选择的对象类型切换到不同的视图.

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.

是否可以通过使用多个 segue 来做到这一点,如果可以,如何实现?

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

推荐答案

当然!通过按住 ctrl 将 从 tableViewController(不是一行,tableViewController 本身)拖到下一个视图,在故事板上定义所有 segue.给他们ID,这样你就知道该打电话给哪一个.当您的所有 segue 都以视觉方式定义后,请转到代码.在您的 tableView 的委托中,在 didSelectRowAtIndexPath 中,只需通过检查 indexPath.row 来调用您想要的 segue:

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;
    }
}

这样,当用户选择第一行时,ID 为Segue0"的 segue 将被触发,依此类推.

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

您还可以在 didSelectRowAtIndexPath 的开头添加一行:[tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] animation:YES]; 所以行用户触摸后不再保持选中状态!

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 !

这适用于静态和动态单元格!小心从 tableViewController 按住 ctrl 拖动你的 segue,而不是单元格!

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

这篇关于我可以在一个 UITableViewDelegate 中使用多个 segue 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆