Xcode 6 - 将 segue 推送到同一个视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26532896/
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
Xcode 6 - Push segue to same view controller
提问by Fergal Rooney
I have a table view controller and tapping on a cell can trigger one of many different types of push segues based on the type of data in the cell. The identifier of the correct segue is determined in tableView:didSelectRowAtIndexPath: and then the segue is triggered with self.performSegueWithIdentifier:
我有一个表格视图控制器,点击一个单元格可以根据单元格中的数据类型触发许多不同类型的推送序列之一。在 tableView:didSelectRowAtIndexPath: 中确定正确 segue 的标识符,然后使用 self.performSegueWithIdentifier 触发 segue:
For one of those segues, I'd like to push another instance of the same view controller on to the navigation stack. Is this possible without having to drag a new view controller object of the same type onto the storyboard? So far, I have only been able to wire up a segue to the same view controller if I Ctrl + Drag from the table cell to the view controller. This is not what I want.
对于其中一个转场,我想将同一视图控制器的另一个实例推送到导航堆栈上。这是可能的,而不必将相同类型的新视图控制器对象拖到情节提要上吗?到目前为止,如果我 Ctrl + 从表格单元格拖动到视图控制器,我只能将 segue 连接到同一个视图控制器。这不是我想要的。
回答by Kyle Parent
As suggested, I don't think a segue will work. Instead you can push a new instance of the same view controller programmatically, which will have the same effect as performing the segue. The difference is that segue delegate methods like prepareForSegue won't be called.
正如所建议的,我认为 segue 不会起作用。相反,您可以以编程方式推送同一视图控制器的新实例,这与执行 segue 具有相同的效果。不同之处在于不会调用像 prepareForSegue 这样的 segue 委托方法。
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyViewController *vc = [[MyViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
回答by Ting Yi Shih
- Create a
Storyboard Reference
in IB. - Refer it to the view controller.
- Then you can create a segue from the view controller to the reference, which refers to itself.
Storyboard Reference
在IB中创建一个。- 请参阅视图控制器。
- 然后你可以创建一个从视图控制器到引用的 segue,它指的是它自己。
回答by afrodev
Complemeting answer above you can do this in Swift 3 using:
完成上面的答案,您可以在 Swift 3 中使用:
let vc = ViewController()
self.navigationController?.show(vc, sender: nil)
If you are using storyboard you can use this:
如果您正在使用故事板,您可以使用这个:
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerID") as! YourViewController
self.navigationController?.show(vc, sender: nil)