1

抱歉措辞糟糕,但在 onAppear 中我正在更改状态变量,但是除非我在视图内的某处使用该状态变量,否则这不会重新渲染视图。

如果用户未登录,我会尝试弹出模式,但除非我将变量放在视图中,否则它不会弹出,如下所示

struct SearchView: View {
// Environment Variables
@EnvironmentObject var session: SessionStore

@State var sheetIsPresented: Bool = false

var body: some View {
    NavigationView {
        // Just having a line like this will cause it to work
        if self.sheetIsPresented || !self.sheetIsPresented {}
    }
    .onAppear {
        if self.session.userSession != nil {
            self.sheetIsPresented = true
        }
    }
    .sheet(isPresented: $sheetIsPresented) {
        WelcomeSignInModal(sheetIsPresented: self.$sheetIsPresented)
    }
}
}
4

1 回答 1

0

您可以尝试以下演示:

class SessionStore: ObservableObject {
    var userSession: String? = "session"
}

struct ContentView: View {
    @EnvironmentObject var session: SessionStore

    @State var sheetIsPresented: Bool = false

    var body: some View {
        NavigationView {
            Text("SearchView")
        }
        .onAppear {
            self.sheetIsPresented = self.session.userSession != nil
        }
        .sheet(isPresented: $sheetIsPresented) {
            Text("WelcomeSignInModal")
        }
    }
}

使用 Xcode 11.6、iOS 13.6 测试

于 2020-08-31T16:23:10.610 回答