SwiftUI: @State属性没有更新,没有奇怪的解决方案



我遇到一个@State属性的奇怪行为,在另一个视图中更改后,在其原始视图中没有正确更新。我使用的是Xcode 12.3和iOS 14。

所发生的是@State "session"基于值的项和"状态流";基于值的项作为绑定参数发送到另一个视图。当点击按钮时,它会改变它们的值,并且原始视图中的fullScreenCover调用应该从switch语句获得流中下一步显示的正确视图。但是"会议"item在该switch语句中为nil,除非我包含一个onChange修饰符,该修饰符查找两个@State属性中的任何一个的更改。onChange调用不需要有任何代码来达到这个效果。

我对SwiftUI还比较陌生(尽管在iOS和Mac开发方面相当有经验)。但这让我很困惑。我不明白为什么它不像预期的那样工作,也不明白为什么添加一个空的onChange处理程序使它工作。

如果您想亲自体验一下,下面是汇编一个简单演示项目的代码:

// the model types
struct ObservationSession: Codable {
public let id: UUID
public var name: String

public init(name: String) {
self.name = name
self.id = UUID()
}
}
struct SessionListModals {
enum Flow: Identifiable {
case configuration
case observation
case newSession

var id: Flow { self }
}
}
// ContentView
struct ContentView: View {
@State private var mutableSession: ObservationSession?
@State private var flow: SessionListModals.Flow?
var body: some View {
VStack {
Button("New Session", action: {
mutableSession = ObservationSession(name: "")
flow = .newSession
})
.padding()
}
.fullScreenCover(item: $flow) {
viewForFlow($0)
}

// Uncomment either of these 2 onChange blocks to see successful execution of this flow
// Why does that make a difference?

//        .onChange(of: mutableSession?.name, perform: { value in
//            //
//        })

//        .onChange(of: flow, perform: { value in
//            //
//        })
}

@ViewBuilder private func viewForFlow(_ flow: SessionListModals.Flow) -> some View {
switch flow {
case .newSession:
// MARK: - Show New Session View
NavigationView {
NewSessionView(session: $mutableSession, flow: $flow)
.navigationTitle("Create a session")
.navigationBarItems(leading: Button("Cancel", action: {
self.flow = nil
}))
}
case .observation:
// MARK: - Show RecordingView
NavigationView {
let name = mutableSession?.name ?? "Unnamed session"
RecordingView(sessionName: name)
.navigationBarItems(leading: Button("Close", action: {
self.flow = nil
}))
}
default:
NavigationView {
EmptyView()
.navigationBarItems(leading: Button("Close", action: {
self.flow = nil
}))
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
// NewSessionView
struct NewSessionView: View {
@Binding var session: ObservationSession?
@Binding var flow: SessionListModals.Flow?
var body: some View {
VStack {
Text("Tap button to create a new session")
Button("New Session", action: {
createNewSession()
})
.padding()
}
}

private func createNewSession() {
let newSession = ObservationSession(name: "Successfully Created A New Session")
session = newSession
flow = .observation
}
}
struct NewSessionView_Previews: PreviewProvider {
static let newSession = ObservationSession(name: "Preview")
static let flow: SessionListModals.Flow = .newSession
static var previews: some View {
NewSessionView(session: .constant(newSession), flow: .constant(flow))
}
}
// RecordingView
struct RecordingView: View {
var sessionName: String
var body: some View {
Text(sessionName)
}
}
struct RecordingView_Previews: PreviewProvider {
static var previews: some View {
RecordingView(sessionName: "Preview")
}
}

class ObservationSession: //Codable, //implement Codable manually
ObservableObject {
public let id: UUID
//This allows you to observe the individual variable
@Published public var name: String

public init(name: String) {
self.name = name
self.id = UUID()
}

}
struct SessionListModals {
enum Flow: Identifiable {
case configuration
case observation
case newSession

var id: Flow { self }
}
}
// ContentView
class ContentViewModel: ObservableObject {
@Published var mutableSession: ObservationSession?

}
struct ContentView: View {
//State stores the entire object and observes it as a whole it does not individually observe its variables that is why .onChange works
@StateObject var vm: ContentView3Model = ContentView3Model()
@State private var flow: SessionListModals.Flow?
var body: some View {
VStack {
Button("New Session", action: {
//Since you want to change it programatically you have to put them in another object
vm.mutableSession = ObservationSession(name: "")
flow = .newSession
})
.padding()
}
.fullScreenCover(item: $flow) {
viewForFlow($0)
}
}

@ViewBuilder private func viewForFlow(_ flow: SessionListModals.Flow) -> some View {
switch flow {
case .newSession:
// MARK: - Show New Session View
NavigationView {
NewSessionView(session: $vm.mutableSession, flow: $flow)
.navigationTitle("Create a session")
.navigationBarItems(leading: Button("Cancel", action: {
self.flow = nil
}))
}
case .observation:
// MARK: - Show RecordingView
NavigationView {
let name = vm.mutableSession?.name ?? "Unnamed session"
RecordingView(sessionName: name)
.navigationBarItems(leading: Button("Close", action: {
self.flow = nil
}))
}
default:
NavigationView {
EmptyView()
.navigationBarItems(leading: Button("Close", action: {
self.flow = nil
}))
}
}
}
}

相关内容

最新更新