我的应用程序分为2个故事板:一个字符选择和一个主应用程序。当用户选择一个字符时,应用程序会继续到主应用程序,并且该故事板的所有视图现在都应该与用户选择的字符相关联。故事板视图控制器之间共享变量?
我想找出共享一个字符串的最佳方式,该字符串将在所有主应用程序故事板视图之间具有所选字符的信息。现在,我使用UserDefaults
只设置一个全局变量:
func loadMainApp(sender: UITapGestureRecognizer) {
let currentCharcter = allCharacters[(sender.view?.tag)!]
let defaults: UserDefaults = UserDefaults.standard
defaults.setValue(currentCharacter, forKey: "CurrentCharacter")
performSegue(withIdentifier: "MainAppSegue", sender: self)
}
从那里在主应用程序情节串连图板中的所有视图控制器可以从UserDefaults
获取字符串。
这是做这种事情的最好方法还是有更好的方法?