SwiftUI:@状态属性未更新,没有奇怪的解决方法

SwiftUI:@状态属性未更新,没有奇怪的解决方法,swiftui,swiftui-state,Swiftui,Swiftui State,我遇到了一个@State属性的奇怪行为,在另一个视图中更改后,该属性在其原始视图中没有正确更新。我使用的是Xcode 12.3和iOS 14 发生的情况是,基于@State“session”值的项和基于@State“flow”值的项作为绑定参数发送到另一个视图。当点击一个按钮时,它会改变它们的值,在原始视图中的全屏覆盖调用应该从switch语句获得正确的视图,以便在流中显示下一个视图。但是在switch语句中“session”项是nil,除非我包含一个onChange修饰符,该修饰符查找两个@S

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

发生的情况是,基于@State“session”值的项和基于@State“flow”值的项作为绑定参数发送到另一个视图。当点击一个按钮时,它会改变它们的值,在原始视图中的全屏覆盖调用应该从switch语句获得正确的视图,以便在流中显示下一个视图。但是在switch语句中“session”项是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
                    }))
            }
        }
    }
}