0

我知道,在 SwiftUI 中,初始启动视图取决于场景委托。所以,有这样的事情:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Create the SwiftUI view that provides the window contents.
        //let contentView = ContentView() //only uncomment when messing with user db
        let contentView = FirstView()
       // let contentView = BarView(value: 3)

        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

我本质上是在告诉 Swift 打开 contentView 的 rootView,在这种情况下它等于 FirstView()。每次打开应用程序时都会运行它,假设它之前已关闭(即不在后台运行)

但是,如果应用程序在后台运行(即您只需在 iPad 上按一次主页按钮或其他任何东西),并且用户再次打开它,应用程序将记住用户上次进入的视图,并恢复该视图。有没有办法改变这种行为?我可以通过某种方式修改场景委托,以便当应用程序在后台运行并且用户打开应用程序时,视图将更改为特定视图?

谢谢。

4

1 回答 1

0

更新,没关系,原谅这个问题。解决方案是将所有代码放入场景委托中的此函数中

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
        print("beans")
    }

此代码将在应用程序进入前台时运行。

我会留下这个问题,以防它帮助其他人!

于 2020-07-16T22:57:02.643 回答