我对编码还很陌生,到目前为止,stackoverflow上的问答对我们很有帮助。我找不到与我的问题有关的任何内容,希望有人知道该怎么办。
本质上;我使用SwiftUI创建了一个应用程序,用户可以在其中存储敏感信息。我正在将可观察对象与motherView一起使用,它会监听可观察对象以让用户登录。由于用户将存储敏感信息,因此,如果我可以在应用程序退出时更改可观察对象,那将是理想选择,因此该应用程序返回登录屏幕(该应用程序将被“锁定”)。
scenedelegate包含在用户退出应用程序时运行的功能:
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
// Save changes in the application's managed object context when the application transitions to the background.
(UIApplication.shared.delegate as? AppDelegate)?.saveContext()
}
我的可观察对象(我想在退出时更改)如下:
class ViewRouter: ObservableObject {
let objectWillChange = PassthroughSubject<ViewRouter,Never>()
var currentPage: String = "page1" {
didSet {
objectWillChange.send(self)
}
}}
page1在MotherView中引用我的LoginView。如果用户输入正确的密码,
currentPage = page2
,然后视图发生变化。我希望它在应用程序退出时返回第1页。
但是,我无法在我的场景委托中声明@ObservedObject var viewRouter: ViewRouter
,然后将self.viewRouter.currentPage = "page1"
添加到函数sceneDidEnterBackground中。
如果我将观察到的对象声明为“场景委托”,则会出现错误“ SceneDelegate类没有初始化程序”。
有人知道如何进行这项工作或如何解决此问题吗?
谢谢!