我试图在两个viewControllers
之间来回传递数据,以便在segueing时维护数据。我有一个mainMenuVC
和一个SettingsVC
。设置在SettingsVC
内选择,然后传递给mainMenuVC
进行存储,但是,这似乎不会发生,我不确定原因。
这是mainMenuVC
中符合transferSettings
协议的代码。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "optionsSegue" {
let receivingVC:SettingsVC = segue.destination as! SettingsVC
receivingVC.settingsDelegate = self
self.settingsDelegate = receivingVC
if self.settingsDelegate != nil{
if self.currentSettings == nil {
print("currentNil")
self.currentSettings = GameOptions()
}
self.currentSettings.description()
self.settingsDelegate.storeGameSettings(settings: self.currentSettings)
}else{ print("settingsDelegate in MainMenuVC nil")}
}
这是`SettingsVC'中的代码,它也符合上面的协议。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "settingsReturn" {
let recievingVC:MainMenuVC = segue.destination as! MainMenuVC
///Set the delegate of MainMenu to SettingsVC
recievingVC.settingsDelegate = self
if self.settingsDelegate != nil {
if self.ScrollView.settingsSelected != nil{
self.ScrollView.saveUserSettings()
print("settingsVC")
self.ScrollView.settingsSelected.description()
self.settingsDelegate.storeGameSettings(settings: self.ScrollView.settingsSelected)
}else{ print("No stored settings in the SettingsVC")}
}else{print("SettingsDelegate in SettingsVC is nil")}
}
}
我认为这样做的方式是我的数据只是在两个viewControllers
之间来回推送,但在用断点检查后,我发现在prepareforsegue
完成执行后,属性存储recievingVC
中的数据为零,从而重置该过程。
我在这里遗漏了什么吗?
谢谢!