简体   繁体   English

从另一个ViewController访问变量

[英]Access Variable from Another ViewController

What is the best way to pass a variable from one ViewController to another in Swift? 在Swift中将变量从一个ViewController传递到另一个ViewController的最佳方法是什么?

I have a ViewController called DatePickerViewController.swift in which I capture the date set by the user with a UIDatePicker object. 我有一个名为DatePickerViewController.swift的ViewController,其中捕获了用户使用UIDatePicker对象设置的日期。

I then need to access the date variable from another view controller. 然后,我需要从另一个视图控制器访问date变量。

Should this be done by passing data forward using Segue's or should I be creating a global variable to access from various ViewControllers? 应该通过使用Segue's向前传递数据来完成此操作,还是应该创建一个全局变量以从各种ViewController访问?

After much research I can't figure this one out. 经过大量研究,我无法弄清楚这一点。 An explanation of the best way to do this would be greatly appreciated. 最好的解释做到这一点的方法将不胜感激。

Many thanks. 非常感谢。

Well, it actually depends from your needs. 好吧,这实际上取决于您的需求。 If you just need to pass one piece of data from a view controller to another, you can simply define a property in your destination view controller and set it in your source view controller before performing the segue (using the func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) method). 如果您只需要将一个数据从视图控制器传递到另一个,则可以在执行segue之前简单地在目标视图控制器中定义一个属性,并在源视图控制器中对其进行设置(使用func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)方法)。

On the other hand, if you have to pass massive data from multiple view controllers, you should consider the idea of creating a Data Model class and storing and retrieving all the information you need from there. 另一方面,如果必须从多个视图控制器传递海量数据,则应考虑创建数据模型类并从那里存储和检索所有需要的信息的想法。

You can access variables and arrays in multiple ViewControllers through NSUserDefaults. 您可以通过NSUserDefaults访问多个ViewController中的变量和数组。

Saving data to NSUserDefaults: 将数据保存到NSUserDefaults:

Before we read or write data from NSUserDefaults, we must first get a reference to the NSUserDefaults class. 在从NSUserDefaults读取或写入数据之前,我们必须首先获取对NSUserDefaults类的引用。

let prefs = NSUserDefaults.standardUserDefaults()

Once we have the reference saved to a constant, we can write data to it. 将引用保存到常量后,就可以向其中写入数据。 You will be defining both the value and the key name to store the data. 您将定义值和键名称以存储数据。

prefs.setValue("Berlin", forKey: "userCity")

Reading data from NSUserDefaults: 从NSUserDefaults读取数据:

Reading data is similar to writing it. 读取数据类似于写入数据。 Again, let's get a reference to the NSUserDefaults class and then we can query a key for a value. 同样,让我们​​获得对NSUserDefaults类的引用,然后我们可以查询键的值。

let prefs = NSUserDefaults.standardUserDefaults()
if let city = prefs.stringForKey("userCity"){
println("The user has a city defined: " + city) 
}else{ 
prefs.setValue("Berlin", forKey: "userCity")  
}

Refer and use the following code in your project 在您的项目中引用并使用以下代码

in FirstViewController 在FirstViewController中

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{
     if segue.identifier == "goNextPage"
     {
        var destinationVC = segue.destinationViewController as? SecondViewController

        destinationVC?.dataFromPreviousViewController = "Success"

     }
}

in SecondViewController 在SecondViewController中

var dataFromPreviousViewController = ""  

override func viewDidLoad() 

{
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    labelGetText.text = dataFromPreviousViewController

    println(labelGetText.text)

}

Important : First of all you have to set segue as Show and give identifier name as goNextPage in StoryboardSegue(whatever you want give here) 重要提示:首先,您必须将segue设置为Show,并在StoryboardSegue 中将标识符名称指定为goNextPage (无论您想在此处提供什么)

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

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