ios 在 Swift 中以编程方式制作 Segue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35349174/
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
Make Segue programmatically in Swift
提问by Kahsn
I have two VCs: VC1 and VC2.
In VC1, I have a finish button
which I programmatically made and a result array
I want to pass to VC2.
我有两个 VC:VC1 和 VC2。在 VC1 中,我有一个finish button
我以编程方式制作的和一个result array
我想传递给 VC2 的。
I know how to make Segue in Storyboard, but I cannot do that at this moment since the finish button
is programmatically made.
我知道如何在 Storyboard 中制作 Segue,但目前我无法这样做,因为它finish button
是通过编程方式制作的。
If I want to pass result array using segue, is there a way to make the segue programmatically? If this is not possible, should I just present VC2 using presentViewController
and set a delegate to pass the result array
?
如果我想使用 segue 传递结果数组,有没有办法以编程方式制作 segue?如果这是不可能的,我是否应该只使用 VC2presentViewController
并设置一个委托来传递result array
?
回答by dehlen
You can do it like proposed in this answer: InstantiateViewControllerWithIdentifier.
您可以按照此答案中的建议进行操作: InstantiateViewControllerWithIdentifier。
Furthermore I am providing you the code from the linked answer rewritten in Swift because the answer in the link was originally written in Objective-C.
此外,我为您提供了用 Swift 重写的链接答案中的代码,因为链接中的答案最初是用 Objective-C 编写的。
let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "identifier") as! SecondViewController
vc.resultsArray = self.resultsArray
EDIT:
编辑:
Since this answer draws some attention I thought I provide you with another more failsafe way. In the above answer the application will crash if the ViewController
with "identifier"is not of type SecondViewController
. In Swift you can prevent this crash by using optional binding:
由于这个答案引起了一些注意,我想我为您提供了另一种更安全的方法。在上面的答案中,如果ViewController
with "identifier"不是 type ,应用程序将崩溃SecondViewController
。在 Swift 中,您可以通过使用可选绑定来防止这种崩溃:
guard let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("identifier") as? SecondViewController else {
print("Could not instantiate view controller with identifier of type SecondViewController")
return
}
vc.resultsArray = self.resultsArray
self.navigationController?.pushViewController(vc, animated:true)
This way the ViewController
is pushed if it is of type SecondViewController
. If can not be casted to SecondViewController
a message is printed and the application remains on the current ViewController
.
这样,ViewController
如果它是 type ,就会被推送SecondViewController
。如果无法强制转换SecondViewController
为消息,则打印并且应用程序保持在当前ViewController
.
回答by creeperspeak
You can still create the segue in Interface Builder by dragging from VC1 to VC2 - just drag from/to the little yellow circle at the top of the VC. Give this segue a unique name in IB, and in your finish
function you can call performSegueWithIdentifier:
, pass in the name of your segue, and that's it. In the prepareForSegue
method you can find out which segue is being performed by accessing segue.identifier
, and if it's the segue in question you can get a pointer to segue.destinationViewController
and pass your data on that way.
您仍然可以通过从 VC1 拖动到 VC2 在 Interface Builder 中创建转场 - 只需从/拖动到 VC 顶部的小黄色圆圈即可。在 IB 中给这个 segue 一个唯一的名称,在你的finish
函数中你可以调用performSegueWithIdentifier:
,传入你的 segue 的名称,就是这样。在该prepareForSegue
方法中,您可以通过访问 找出正在执行哪个转场segue.identifier
,如果是有问题的转场,您可以获得指向segue.destinationViewController
并以这种方式传递数据的指针。