ios 使用 segue 在视图控制器之间传递变量

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

Passing variables between View Controllers using a segue

iosxcodeswiftseguexcode6

提问by DanteDeveloper

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 将变量从一个视图控制器传递到另一个视图控制器。

I have created a segue called 'MainToTimer' which is trigger once button is pressed. I would like to be able to use the variable called 'Duration' on the second View Controller.

我创建了一个名为“MainToTimer”的 segue,它在按下按钮时触发。我希望能够在第二个视图控制器上使用名为“持续时间”的变量。

Is it possible to pass multiple variables and constants?

是否可以传递多个变量和常量?

What code do I need associated to each View Controller?

我需要与每个视图控制器相关联的代码是什么?

Thank you in advance.

先感谢您。

回答by Donn

First, setup property/properties to hold your variables in your second view controller (destination).

首先,设置 property/properties 以将变量保存在第二个视图控制器(目标)中。

class YourSecondViewController: UIViewController {
    var duration:Double?
}

Then have your button trigger your custom 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. If you have multiple data you want to pass in, put them in an array and make that array the sender.

是的,也可以传递多个变量和常量,再次使用 prepareForSegue 的“sender”参数。如果您有多个要传入的数据,请将它们放入一个数组中并使该数组成为发送方。

SWIFT 3From 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?)

回答by Steve Rosenberg

In the first ViewControllerplace this (for modal segue):

首先ViewController这个(对于模态segue):

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

Change ViewController2to the name of the second ViewController. In ViewController2create a class variable:

更改ViewController2为第二个的名称ViewController。在ViewController2创建类变量时:

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

That's it. You will have in the value of Duration2the value of Durationfrom the first ViewController.

就是这样。您将拥有从第一个值开始Duration2的值。DurationViewController