私のアプリでは、データをuserDefaultsに保存しました。同じキーでデータを更新しようとすると、更新されますが、シミュレータを再起動します。そうでなければ、私はすぐにそれにアクセスしたいのですが(それは他のView Controllerです)、更新されたオブジェクトを私に渡すのではなく、そのキーで保存した最後のデータを返します。 1つのビューコントローラ内のデータをoverride func viewWillDisappear(_ animated :)
に更新し、親ビューコントローラでそのデータにアクセスしようとしました。ここでユーザーからのデータにアクセスできないアプリケーションのライフサイクルでのデフォルト
である私がどのように:
override func viewWillDisappear(_ animated : Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParentViewController {
if saveButtonPressed != true {
// here I don't need to execute code the back button is pressed
} else {
if let selection = selectedMinutes {
UserDefaults.standard.set(selection, forKey: "selectedRow")
print(UserDefaults.standard.object(forKey: "selectedRow")!) // this line prints the actual updated data but I still cannot get it in the parent view controller
}
}
}
}
後UserDefaultsを同期呼び出す必要がありますか? – Leonardo
親ビューコントローラは実際には以前のコントローラであり、現在のビューコントローラをポップするだけで、そのような親ビューコントローラにアプリが入ります。 – Viktor
'UserDefaults'を使ってコントローラ間でデータを共有しないでください。値を親コントローラに直接渡すことができるように、親ビューコントローラへの参照が必要です。 – vadian