1

I have 3 View Controllers in Storyboard. They're named "Letters", "Numbers", and "Other".

What I want is a variable that changes based on which category was used last.

Ex: The user goes to the Letters View Controller (var categories = "Letters"), and then to the Other View Controller, and is able to use the "categories" variable (which is still "Letters"). User goes to the Numbers View Controller (var categories = "Numbers"), and then to the Other View Controller, and is able to use the categories variable (which is now "Numbers").

I'm not sure if my question and example are clear enough, and if they aren't feel free to ask, and I'll try to further explain.

Any help would be greatly appreciated.

Thanks in advance.

1
  • Sounds like you want to pass the 'categories' variable between your view controllers. You can do this when you are presenting each view controller. I will post an answer below Commented Apr 12, 2017 at 18:36

2 Answers 2

3

There are lots of ways to do this, but the key concept is that you don't change variables in other classes, you change things in instances (objects) of classes.

I am going to try to keep this simple.

So, you could make a class that has App State kinds of things

public class AppState {
    public var category: String? = nil
    public static let shared = AppState()
}

Then, anywhere in your code, you can do

AppState.shared.category = "Letters"

Or

if let currentCategory = AppState.shared.category {
    // use currentCategory here
}

Doing this is not the best way, but it will work and it's simple. To go a little further, you could learn about dependency injection. Using that, there would not be a global shared instance, but instead you would pass the appropriate AppState object to objects that needed it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I'll look into it right away. And get back to you if it works the way I need it.
It works beautifully. However, will the optional get in the way if I use the String in a switch statement, and if so, whats the best way to unwrap it?
If you have a default category you do not need to make category optional. Assign the default and you do not need unwrapping anymore. That removes the possibility of setting nil accidentally later.
Oh alright thanks! Not sure why I didn't think of that myself =]
0

The best way to do this is by creating a variable named categories on each of your view controllers and assigning a value to this before you present your next view controller.

EXAMPLE:
Letters View Controller

var categories = [String]()

    func goToNextViewController()
    {
      let numbersVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "numbersVC")
      numbersVC.categories = self.categories
      self.presentViewController(numbersVC, animated: true, completion: nil)
    }

Numbers View Controller

var categories = [String]()

2 Comments

Thanks for the quick reply. I'll check each answer and see which one suits my program best (most versatile).
No problem. As Lou Franco hints, global states should be avoided if possible.

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.