ios Storyboard Segue 从视图控制器到它本身

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9226983/
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:45:27  来源:igfitidea点击:

Storyboard Segue From View Controller to Itself

iosuitableviewuiviewcontrollerstoryboardsegue

提问by Jorge

I am trying to make a mechanism to drill down a file / folder list. The idea is to show the same file list view controller every time the user selects a folder, and show a file detail view controller if he/she selects a file.

我正在尝试制作一种机制来深入了解文件/文件夹列表。这个想法是在用户每次选择一个文件夹时显示相同的文件列表视图控制器,如果他/她选择一个文件,则显示一个文件详细信息视图控制器。

So far, I have created a segue from the file list view controller to the file detail view controller, and a segue from the file list table view cell to the the file list table view controller:

到目前为止,我已经创建了一个从文件列表视图控制器到文件详细视图控制器的 segue,以及一个从文件列表视图单元格到文件列表视图控制器的 segue:

enter image description here

在此处输入图片说明

The issue with this is that as soon as the user taps the cell, the segue is executed. I would like to remove the segue from the table view cell and make one from the file list view controller to itself. That way, I could trigger the right segue programmatically when the user tapped the cell.

这样做的问题是,只要用户点击单元格,就会执行 segue。我想从表格视图单元格中删除 segue 并从文件列表视图控制器中创建一个到它自己。这样,当用户点击单元格时,我可以以编程方式触发正确的转场。

So, my question is: Is it possible to create a segue from a view controller to itself in Interface Builder?

所以,我的问题是:是否可以在 Interface Builder 中创建从视图控制器到自身的 segue?

采纳答案by T.J.

I developed a method to create a segue using a phantom button. I believe it will solve your problem. You can read about it in my answer here.

我开发了一种使用幻像按钮创建转场的方法。我相信它会解决你的问题。你可以在我的答案读到它在这里

回答by Jim True

If you are using a navigation controller you need to push the ViewController into the nav stack. In this example, i named my ViewController "VDI" in my Storyboard ID setting.

如果您使用导航控制器,则需要将 ViewController 推送到导航堆栈中。在这个例子中,我在我的故事板 ID 设置中将我的 ViewController 命名为“VDI”。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
YourVC *dest = [storyboard instantiateViewControllerWithIdentifier:@"VDI"];
[self.navigationController pushViewController:dest animated:YES];

If you don't want the NavigationController to keep adding itself into your "Back" history you can pop the stack before adding to it like so.

如果您不希望 NavigationController 继续将自己添加到您的“返回”历史记录中,您可以像这样在添加之前弹出堆栈。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
YourVC *dest = [storyboard instantiateViewControllerWithIdentifier:@"VDI"];
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:dest animated:YES];

回答by John Henckel

Using Xcode 5 there is a muchsimpler solution.

使用 Xcode 5 有一个简单的解决方案。

  1. Click the table cell in the storyboard
  2. Open the Connections Inspector (right arrow icon in the upper right)
  3. Under "triggered segues" you see "selection"
  4. Drag from the circle next to "selection" to the cell in the storyboard
  1. 单击故事板中的表格单元格
  2. 打开连接检查器(右上角的右箭头图标)
  3. 在“触发转场”下,您会看到“选择”
  4. 从“选择”旁边的圆圈拖动到故事板中的单元格

That's it.

就是这样。

回答by Mustafa

Instead of performing a segue to the same controller, you can instantiate a view controller (the same one) from storyboard, and then push that onto the navigation controller.

您可以从故事板实例化一个视图控制器(同一个),而不是对同一个控制器执行转场,然后将其推送到导航控制器上。

回答by Joshua C. Lerner

Interface Builder approach: Just segue to a storyboard reference which refers back to the presenting view controller.

Interface Builder 方法:只需转至一个故事板参考,该参考回指呈现视图控制器。

回答by Sethmr

The correct answer is to use a Storyboard Reference that is referencing the UIViewController you want to segue to itself and then point the segue at it.

正确的答案是使用一个 Storyboard Reference,它引用了你想要转场的 UIViewController,然后将转场指向它。

enter image description here

在此处输入图片说明

回答by Chris

In IOS 6, there is a cleaner solutionthan using a phantom button. You can still define the segue from the table cell to the view controller, and look at the sender to cancel the automatically triggered segue:

在 IOS 6 中,有一个比使用幻象按钮更简洁的解决方案。您仍然可以定义从表格单元格到视图控制器的segue,并查看发送方取消自动触发的segue:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //storyboards should use segues and override prepareForSegue instead
    //but here we need custom logic to determine which segue to use
    id item = [self.fetchedResultsController objectAtIndexPath:indexPath];
    if (item meets condition) {
        [self performSegueWithIdentifier:@"segue1" sender:self];
    } else {
        [self performSegueWithIdentifier:@"segue2" sender:self];
    }
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    //ignore segue from cell since we we are calling manually in didSelectRowAtIndexPath
    return (sender == self);
}

回答by Tomas Andrle

Here's how you can push another instance of the current view controller without defining a segue or hardcoding its own identifier:

以下是如何推送当前视图控制器的另一个实例而无需定义 segue 或硬编码其自己的标识符:

SameViewController *same = [self.storyboard instantiateViewControllerWithIdentifier: self.restorationIdentifier];
[self.navigationController pushViewController: same animated: YES];

You just need to set the Restoration ID to be the same as Storyboard ID (there's a checkbox for that in IB).

您只需要将恢复 ID 设置为与故事板 ID 相同(IB 中有一个复选框)。

Restoration ID

恢复ID

回答by Yari

Hope this helps.

希望这可以帮助。

I found that you can create multiple prototype cells.

我发现您可以创建多个原型单元格。

Than you can link every cell (in the Storyboard) to a different View.

您可以将每个单元格(在故事板中)链接到不同的视图。

Something like this:

像这样的东西:

NSString *CellIdentifier = @"Cell"; 
if (Condition2 ){
CellIdentifier = @"Cell2"; } 
if (Condition3 ){
CellIdentifier = @"Cell3"; }