Reputation: 2492
On my iOS app written in Swift, I have a variable which is initialized on FirstViewController.swift
.
I want to assign its value to a label on SecondViewController.swift
.
At first I've tried to do it like this on SecondViewController.swift
:
var firstViewController: FirstViewController = FirstViewController(nibName: nil, bundle: nil)
var name = firstViewController.name
After the didn't work, I tried to do it using a struct
:
// FirstViewController.swift
struct GlobalVariables {
var name: String = "test"
}
// SecondViewController.swift
var name = FirstViewController.GlobalVariables.name
But that didn't work either. After both methods I'm printing the new variable to the console and assign its value to the label, but all I see is nothing on the label and nil
on the console.
Can you please help me with that? How can I access to a variable on FirstViewController.swift
through SecondViewController.swift
?
Upvotes: 0
Views: 5845
Reputation: 455
To pass arguments between View Controllers, you can use segues.
First you have the variable in FirstViewController
FirstViewController: UIViewController {
var name: String = "test"
...
}
Then you have a variable of the same type in SecondViewController
SecondViewController: UIViewController {
var name: String = ""
...
}
To move from FirstViewController, you use a programmatic segue.
self.performSegue(withIdentifier: "Indentifier", sender: nil)
Then, in FirstViewController, define prepareForSegue:sender. You get a reference to the destination view controller, then set the variable to the one from sending view controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! SecondViewController
vc.name = name
}
EDIT: If you need it to be accessible in all view controllers, define a Global class. You stated in your question you tried a struct. Instead, try static variables
class Global {
static var name: String?
}
EDIT 2: Another way to have global variables is with a singleton class
class Global {
static let sharedInstance = Global()
var name: String?
}
Upvotes: 6
Reputation: 755
Here's a barebones idea of how to do it with a separate file to hold data (values) for your labels to display. Disclaimer: it might not be the best idea (you probably don't want to bind your ViewControllers to this single data file like this), but it should do the trick before you learn other better ways. So, here we go.
You want to use a separate file (name it for example MyTextValuesModel.swift
) for all the values you'd like your FirstViewController
and SecondViewController
to access.
You select File > New > File... from the menu, and choose Swift File.
Then you declare your class
:
import Foundation
class ValuesForLabels {
var textForLabel1 = "Label1"
var textForLabel2 = "Label2"
var textForLabel3 = "Label3"
// etc...
}
Now in your FirstViewController
and SecondViewController
you declare a variable to refer to this class:
var textsForLabels = ValuesForLabels()
Now you can access (read/write) the labels from any view controllers like this:
textsForLabels.textForLabel1 = "NewLabel1Text"
// you've just set a new value for Label1
label1.text = textsForLabels.textForLabel1
// your label1 text is now set to textForLabel1
If you'd want to access label values from another view controller, add a new variable to your new view controller to reference the labels.
Upvotes: 0