我想在两个接口控制器之间传递一个字符串。在InterfaceController1上,我想创建一个如下变量:

var level:String = ("easy")

然后能够访问interfacecontroller2上的变量。
我不希望使用全局变量,因为我知道不建议使用它们。
StackOverflow上有一个答案,但它是在Swift 1出局时创建的,我找不到任何最新的Anwser。
谢谢

最佳答案

你有几个选择来实现这个目标。
使用pushController(withName:,context:)并将level设置为上下文输入参数,然后在InterfaceController2的awake(withContext:)中使用该变量。
如果需要从多个类访问InterfaceController2的变量,或者如果不直接导航到InterfaceController2,请使用singleton(将变量声明为static)。
如果只需要从InterfaceController1设置该变量,并且直接从InterfaceController1显示InterfaceController2,则选项1是首选方法。

09-25 18:27