简体   繁体   English

使用segue在View Controllers之间传递变量

[英]Passing variables between View Controllers using a segue

I use Swift and Xcode 6 and would like to pass a variable from one View Controller to another using a Segue. 我使用Swift和Xcode 6,并希望使用Segue将变量从一个View Controller传递给另一个。

I have created a segue called 'MainToTimer' which is trigger once button is pressed. 我创建了一个名为'MainToTimer'的segue,按下按钮后触发。 I would like to be able to use the variable called 'Duration' on the second View Controller. 我希望能够在第二个View Controller上使用名为'Duration'的变量。

Is it possible to pass multiple variables and constants? 是否可以传递多个变量和常量?

What code do I need associated to each View Controller? 我需要为每个View Controller关联哪些代码?

Thank you in advance. 先感谢您。

First, setup property/properties to hold your variables in your second view controller (destination). 首先,设置属性/属性以将变量保存在第二个视图控制器(目标)中。

class YourSecondViewController: UIViewController {
    var duration:Double?
}

Then have your button trigger your custom segue. 然后让您的按钮触发您的自定义segue。 Use your variable ('duration') as the argument for sender. 使用您的变量('duration')作为发件人的参数。

class YourFirstViewController: UIViewController {
    @IBAction func buttonTapped(sender: AnyObject) {
        self.performSegueWithIdentifier("MainToTimer", sender: duration)
    }
}

Finally, pass this sender data by overriding the prepareForSegue method: 最后,通过覆盖prepareForSegue方法传递此发送方数据:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "MainToTimer") {
        let secondViewController = segue.destinationViewController as YourSecondViewController
        let duration = sender as Double
        secondViewController.duration = duration
    }
}

Yes, it is also possible to pass multiple variables and constants, again using the 'sender' parameter of prepareForSegue. 是的,也可以使用prepareForSegue的'sender'参数传递多个变量和常量。 If you have multiple data you want to pass in, put them in an array and make that array the sender. 如果要传入多个数据,请将它们放在一个数组中,并使该数组成为发送方。

SWIFT 3 From Swift 3, the method prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) has changed to prepare(for segue: UIStoryboardSegue, sender: Any?) SWIFT 3从Swift 3开始,方法prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)已经改为prepare(for segue: UIStoryboardSegue, sender: Any?)

In the first ViewController place this (for modal segue): 在第一个ViewController放置这个(用于模态segue):

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let theDestination = (segue.destinationViewController as ViewController2)
    theDestination.Duration2 = Duration
}

Change ViewController2 to the name of the second ViewController . ViewController2更改为第二个ViewController的名称。 In ViewController2 create a class variable: ViewController2创建一个类变量:

var Duration2 = (whatever the type - UInt8 I guess for time)

That's it. 而已。 You will have in the value of Duration2 the value of Duration from the first ViewController . 您将在价值Duration2的值Duration从第一个ViewController

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

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