0

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?

8
  • See How to pass data between view-controllers Commented May 3, 2017 at 18:22
  • What is the context of trying to share this info? Are you performing a segue? Are both VC instantiated? Commented May 3, 2017 at 18:23
  • 1
    @DonovanKing I'm doing a segue from FirstViewController.swift to SecondViewController.swift using a button. I don't want to pass the variable. I need the variable to be available from all ViewControllers without passing it Commented May 3, 2017 at 18:26
  • You'd probably want to create a model object (or a struct) to hold this piece of data and access it by the two ViewControllers. Commented May 3, 2017 at 18:28
  • 1
    @SimpleBeat Could you please instruct me how? I'm new to iOS development Commented May 3, 2017 at 18:37

2 Answers 2

6

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?
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is not what I want. Look at the comments. I need to be accessible from any ViewController. Not just this one
@IdoNaveh: If the variable is a class you're only passing a reference which means both VCs will be pointing to the same instance. Is there a particular reason you have not to pass a reference? You really shouldn't be pollution the global namespace.
0

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.

1 Comment

I have just tried it, but I keep on getting nil value from these variables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.