IOS StoryBoard 来自 TableCell 的多个 Segue

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8837944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:15:28  来源:igfitidea点击:

IOS StoryBoard multiple Segue's from a TableCell

iosstoryboardsegue

提问by Bear

Hi I have a storyboard and am able to show a detail view when clicking on a table cell. I want to add extra functionality so that depending on what cell I click I show a different view controller. I tried dragging two segues from the same cell but it doesn't allow it.

嗨,我有一个故事板,可以在单击表格单元格时显示详细信息视图。我想添加额外的功能,以便根据我单击的单元格显示不同的视图控制器。我尝试从同一个单元格中拖动两个 segue,但它不允许。

My thinking was that I would have two segue's from the cell each pointing to a different view and then invoke the desired segue:

我的想法是,我会从单元格中获得两个 segue,每个 segue 指向不同的视图,然后调用所需的 segue:

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

    NSInteger row = indexPath.row;
    NSLog(@"Selected Item :-) %@",[NSString stringWithFormat:@"%@",[myData objectAtIndex:row]]);
    if(row %2 ==0){
        NSLog(@"Even");        
        [self performSegueWithIdentifier:@"ShowSecondIndex" sender:self];
    }else{
        [self performSegueWithIdentifier:@"ShowSelectedMovie" sender:self];
        NSLog(@"Odd");

    }

} 

I would then handle the segue in prepareForSegue

然后我会在 prepareForSegue 中处理 segue

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

    NSLog(@"Prepare For Segue ID:%@",[segue identifier]);

    if([[segue identifier] isEqualToString:@"ShowSelectedMovie"]){
        Tab2_ItemViewController *vc = [segue destinationViewController];
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        NSLog(@"Selected Index: %d",selectedIndex);
        [vc setSelectedItem: [NSString stringWithFormat:@"%@",[myData objectAtIndex:selectedIndex]]];
        NSLog(@"String Value: %@",[NSString stringWithFormat:@"%@",[myData objectAtIndex:selectedIndex]]);
        [vc setSelectedIndex:selectedIndex];


    }else if([[segue identifier] isEqualToString:@"ShowSecondIndex"]){

        NSLog(@"Viewing Second Index");
    }

}

However it never shows the second view. Is this because its not possible to have two segues from a single table cell. I also tried dragging both segue's from the controller to each destination rather than one from the cell and one from the controller but sill no luck???

但是它永远不会显示第二个视图。这是因为单个表格单元格不可能有两个 segue。我还尝试将两个 segue 从控制器拖到每个目的地,而不是从单元拖到一个,从控制器拖一个,但仍然没有运气???

回答by LJ Wilson

Don't try to hook up the Segues to a tableviewcell in this case. Hook them up to the View Controller itself.

在这种情况下,不要尝试将 Segue 连接到 tableviewcell。将它们连接到视图控制器本身。

回答by mgrandi

Don't try to create multiple segues from a TableCell to other view controllers, you want to ctrl+drag from the view controller icon below the view controller in the storyboard interface to the viewcontrollers you want to segue to. Then it will allow you to set up multiple segues.

不要尝试从 TableCell 到其他视图控制器创建多个 segue,您希望从故事板界面中视图控制器下方的视图控制器图标按住 ctrl+drag 到您想要 segue 的视图控制器。然后它将允许您设置多个 segue。

screenshot showing multiple segues

显示多个 segue 的屏幕截图

and then to actually make the segues work, you need to add identifiers to the segues themselves, which you can do by clicking on them and then giving it a name in the property inspector:

然后要真正使 segue 工作,您需要向 segue 本身添加标识符,您可以通过单击它们然后在属性检查器中为其命名来完成此操作:

giving segue an identifier

给 segue 一个标识符

then, for the example of TableCells, in your UITableViewDelegate, in

然后,对于 TableCells 的示例,在您的 UITableViewDelegate 中,在

-tableView:didSelectRowAtIndexPath: 

you can use

您可以使用

- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender 

to manually start a segue depending on your own logic of what segue should be chosen.

根据您自己的应该选择什么转场的逻辑手动启动转场。

回答by aztack

Here's a sample code from my demo project:

这是我的演示项目中的示例代码:

-         (void)tableView:(UITableView *)tableView
  didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *segueName = nil;

    if (type == kCore) {
        segueName = @"segue1";
    } else if (type == kStdlib) {
        segueName = @"segue2";
    }

    [self performSegueWithIdentifier: segueName sender: self];
}

typeis a property of view controller, which determines which segue should be performed.

type是视图控制器的一个属性,它决定了应该执行哪个转场。

As the above answer said, the key is to create segue by linking two view controllers.

正如上面的答案所说,关键是通过链接两个视图控制器来创建 segue。

回答by Jayce

In swift 3.1, with a segment control of two states

在 swift 3.1 中,具有两种状态的段控制

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var segue: String!
if selectedSegment == 0 {
    segue = "segue1"
} else  {
    segue = "segue2"
}
self.performSegue(withIdentifier: segue, sender: self)

}

}