@State not updating in SwiftUI 2

青春壹個敷衍的年華 提交于 2020-12-05 11:17:28

问题


Anybody see problems with @State variables not updating in SwiftUI 2 in Xcode 12b? The problem code is in the contextMenu buttons.

The showSheet var is toggled to true but the sheetItem value is not changed. This same code worked in SwiftUI 1.

I have filed a Feedback, but was wondering if anyone else has seen this problem.

Full code:

struct ConnectionsView: View {

@EnvironmentObject var connectionViewModel : FileDataViewModel<Connection>

@State private var editMode = EditMode.inactive
@State private var importedConnections = false
@State private var showEditSheet = false
@State private var selectedIndex = 0
@State private var showSheet = false
@State private var sheetItem = SheetItem.openFile //TOD: Set back to .none when ButtonActions fixed
@State private var newConnection = Connection()

var body : some View {
    NavigationView {
        List {
            ForEach(connectionViewModel.items) { connection in
                NavigationLink (destination: SSHSessionView (connection: connection)) {
                    ConnectionView(connection: connection)
                        //pass tap through the view to allow the NavigationLink to work
                        .allowsHitTesting(self.editMode == .inactive ? false : true)
                        .onTapGesture {
                            if self.editMode.isEditing {
                                self.selectedIndex = self.connectionViewModel.items.firstIndex(where: {$0.id == connection.id})!
                                self.sheetItem = .editItem
                                self.showSheet.toggle()
                            }
                        }
                }
            }
            .onDelete(perform: deleteItem)
            .onMove(perform: moveItem)
        }
        .onAppear {
            UITableView.appearance().tableFooterView = UIView()
        }
        .navigationBarTitle("Connections", displayMode: .large)
        .navigationBarItems(
            leading:
                EditButton()
                    .hoverWithPaddingModifier(),
            trailing:
                Button(action: {
                }, label: {
                    Image(systemName: "plus")
                })
                .contextMenu {
                    Button("New Connections file...", action: {
                        sheetItem = .newFile
                        showSheet.toggle()
                    })
                    Button("Open Connections file...", action: {
                        sheetItem = .openFile
                        showSheet = true
                    })
                    if connectionViewModel.fileOpened {
                        Button("New Connection", action: {
                            sheetItem = .newItem
                            showSheet.toggle()
                        })
                    }
                }
        )
            
        .environment(\.editMode, $editMode)
        
        .sheet(isPresented: $showSheet, content: {
            
            if self.sheetItem == .editItem {
                AddEditConnectionView (connection: self.$connectionViewModel.items[self.selectedIndex], newConnection: false)
                    .environmentObject(self.connectionViewModel)
            }
            
            if self.sheetItem == .newFile {
                FilePickerView(callback : self.importedConnections == false ? self.pickedDocuments : self.newDocFromServerList, UTIs : connectionUTIs, newFileURL : FileDataViewModel<Connection>.getBlankFile (fileType : ViewTypes.connections)!)
            }
            
            if self.sheetItem == .openFile {
                FilePickerView(callback : self.pickedDocuments, UTIs : connectionUTIs, newFileURL: nil)
            }

            if self.sheetItem == .newItem {
                AddEditConnectionView (connection: self.$newConnection, newConnection: true)
                    .environmentObject(self.connectionViewModel)
            }
        })
    }
}

来源:https://stackoverflow.com/questions/62666415/state-not-updating-in-swiftui-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!