如何在SceneDelegate中更改@ObservedObject变量?

时间:2020-07-02 09:30:43

标签: ios swift swiftui

我在这里有一个模型:

class Instance: ObservableObject {
    @Published var status: Status = .offline

当更改时会更新以下视图...

struct HomeView: View {
    @ObservedObject var instance = Instance()
    var body: some View {
        Text(self.instance.status.text)
    }
}

我想在我的应用打开时更改status的值。例如,在SceneDelegate中:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Change Instance variable status to .online
    }

我该如何实现?

编辑:

struct ContentView: View {
    @ObservedObject var auth = UserAuth()
    var body: some View {
        Group {
            if auth.uid != nil { HomeView(auth: auth) } else { LoginView(auth: auth) }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您只需将实例移动到SceneDelegate中,然后将引用传递给HomeView。您可以根据需要将其直接传递或将实例放入环境中。以下是直接参考的示例:

<header>
    Header
</header>
<main>
<ul>
    <li><a href="#one">One</a></li>
    <li><a href="#two">Two</a></li>
    <li><a href="#three">Three</a></li>
    <li><a href="#four">Four</a></li>
    <li><a href="#five">Five</a></li>
</ul>
<p id="one">One: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p><a name="two"></a>Two: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p id="three">Three: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p><a name="four"></a>Four: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p id="five">Five: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<!-- So there's room to scroll at the bottom -->
<div style="min-height: 100vh"></div>
</main>

更改您的视图,以使其不会自行初始化实例:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    var instance = Instance()
    ...

    let contentView = HomeView(instance: self.instance)

    ...
    func sceneWillEnterForeground(_ scene: UIScene) {
        instance.status = ...
    }
    ...

答案 1 :(得分:2)

如果我正确理解了您的代码,在这种情况下,最合适的是将wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class='x28o xfn p_AFTextOnly']"))) 实例保留在auth

SceneDelegate

并通过注入class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? let auth = UserAuth() func sceneWillEnterForeground(_ scene: UIScene) { // Change Instance variable status to .online auth.status = .online }

将其用作@EnvironmentObject