简体   繁体   English

Swift-通过Segue传递值

[英]Swift - passing values via segue

I'm trying to pass few variables via a segue. 我试图通过segue传递一些变量。 Initially I capture 6 variables on the first screen which I would like to pass on to the second view controller. 最初,我在第一个屏幕上捕获了6个变量,并将它们传递给第二个视图控制器。

Each variable is captured through a text box capturing an integer and I called them T1, T2, T3 ... T6. 每个变量都是通过捕获整数的文本框捕获的,我称它们为T1,T2,T3 ... T6。 At present I refer to the value through T1.text.toInt()!. 目前,我通过T1.text.toInt()!引用该值。 Before I pass these values via segue, should I first create a variable like var T1 = T1.text.toInt()! 在通过segue传递这些值之前,我应该先创建一个像var T1 = T1.text.toInt()的变量! ?

What is the best way of designing this? 设计此的最佳方法是什么?

inside prepareForSegue you have access to the UIController instance that will open: 在prepareForSegue内,您可以访问将打开的UIController实例:

class FirstPageUIViewController:UIViewController {
...

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    var controller = (segue.destinationViewController as! MyNextViewController)
    controller.T1 = "value to pass"
  }

}

This means you define you variables in MyNextViewController (the controller for your second screen) and the variables are already set when your MyNextViewController instance takes over control. 这意味着您可以在MyNextViewController(第二个屏幕的控制器)中定义变量,并且在MyNextViewController实例接管控制权时已经设置了变量。

You also might decide to name your variables starting with small letters according to the swift style guide. 您还可以根据快速样式指南决定以小写字母开头来命名变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM