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?
FirstViewController.swifttoSecondViewController.swiftusing a button. I don't want to pass the variable. I need the variable to be available from all ViewControllers without passing it