2

I want to pass a string between two Interface Controllers. On InterfaceController1 I want to create a variable like:

var level:String = ("easy")

and then be able to access that variable on InterfaceController2.

I would prefer not to use global variables as I am aware that they are not recommended.

There is one answer on StackOverFlow but it was created when Swift 1 was out and I can't find any up to date anwser.

Thanks

1 Answers1

0

You have several options to achieve this.

  1. Use pushController(withName:,context:) and set level as the context input argument, then in InterfaceController2's awake(withContext:) use that variable.
  2. Use singletons (declare the variable as static) if you need to access the variable of InterfaceController2 from several classes or if you don't directly navigate to InterfaceController2.

If you only need to set up that variable from InterfaceController1 and you directly present InterfaceController2 from InterfaceController1, option 1 is the preferred method.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116