[英]Trying to pass state variable to automatically update view in SwiftUI
I have a variable that holds the state of whether or not a certain type of session is active.我有一个变量,它保存 state 的某种类型的 session 是否处于活动状态。 This is used to determine which view is shown.这用于确定显示哪个视图。 I want my view model to post a document, toggle this state, and then automatically update the view.我想让我的view model发一个文档,切换这个state,然后自动更新view。 However, the view is not updating.但是,视图没有更新。
So this is the base layer of the view that I have.所以这是我所拥有的视图的基础层。 sessionState starts as false and defaults to the StartSessionView to initialize a session. If one is active, it goes to GroupSessionView. sessionState一开始是false,默认是StartSessionView,初始化一个session,如果是active,就去GroupSessionView。
struct SessionView: View {
@State var sessionState = false
@ViewBuilder
var body: some View {
if sessionState {
StartSessionView(sessionState: $sessionState)
} else {
GroupSessionView(sessionState: $sessionState)
}
}
}
The StartSessionView page has the following code. StartSessionView 页面具有以下代码。 The important thing here is the sessionManager view model. I initialize a SessionManager with a Session object (not important as that part is working), and a sessionState.这里重要的是 sessionManager 视图 model。我使用 Session object(不重要,因为该部分正在工作)和 sessionState 初始化 SessionManager。
struct StartSessionView: View {
@State var sessionManager = SessionManager(session: Session(sessionType: ""), sessionState: false)
@ObservedObject var userViewModel = AuthViewModel()
@Binding var sessionState: Bool
var body: some View {
VStack(alignment: .leading, spacing: 40) {
Button {
sessionManager = SessionManager(session: Session(sessionType: dropdownValue), sessionState: sessionState)
sessionManager.startSession()
} label: {
Text("Do stuff")
}
}
}
}
And finally, this is the view model.最后,这是视图 model。
class SessionManager: ObservableObject {
@State var sessionState: Bool
let session: Session
init(session: Session, sessionState: Bool) {
self.session = session
self.sessionState = sessionState
}
func startSession() {
var messageData = ["sessionType": session.sessionType]
let document = Firestore.firestore()
.collection("sessions")
.document()
document.setData(messageData) {error in
if let error = error {
print(error)
return
} else { sessionState.toggle }
}
}
}
So when the sessionState is toggled here, I want this to be passed back to the SessionView struct, see that sessionState = true, and go to the GroupSessionView.因此,当在这里切换 sessionState 时,我希望将其传递回 SessionView 结构,看到 sessionState = true,并将 go 传递给 GroupSessionView。 I'm not sure how to pass this data through so many layers though.我不确定如何通过这么多层传递这些数据。
Oops, It seems you missing something.哎呀,看来你错过了什么。 @State only works with variable, structs.. and not a class in this case
. @State only works with variable, structs.. and not a class in this case
。 And if you want to pass data to another view you could be using the environment in SwiftUI.如果你想将数据传递给另一个视图,你可以使用 SwiftUI 中的环境。
struct SessionView: View {
@State var sessionState = false
@StateObject var someThingKeepManager = SomeThingKeepManager()
@ViewBuilder
var body: some View {
ZStack {
if sessionState {
StartSessionView(sessionState: $sessionState)
} else {
GroupSessionView(sessionState: $sessionState)
}
}
.environmentObject(someThingKeepManager)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.