SwiftUI:解雇工作表后,navigationBarItems中的所有按钮不再起作用

时间:2020-01-11 17:03:56

标签: swiftui

我解雇一张纸后,以上屏幕上的按钮不再起作用。仅在按下非相互作用的表面后,按钮才能再次起作用。我使用的是Swift 5版本,该错误发生在模拟器和设备上。

#Edit

代码段

AddView,它将显示在工作表中

Le volume dans le lecteur E s'appelle xxxxxxxx
Le numéro de série du volume est xxxxxxxx

Répertoire de E:\TEST

11/03/2018  20:45     1 947 028 131 test.mkv
               1 fichier(s)    1 947 028 131 octets
               0 Rép(s)  64 368 631 808 octets libres

ContentView包含具有某些功能的FetchRequest,仅此而已。

struct AddView: View {
    @Environment(\.managedObjectContext) var moc
    @Environment(\.presentationMode) var presentationMode

    // some state

    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Name")) {
                    TextField("Task-Name (e.g. Eat the ?)", text: $title)
                }

                Section(header: Text("Settings")) {
                    DatePicker("Date", selection: $timestamp, displayedComponents: .date)
                    Toggle(isOn: $highPrio) {
                        Text("High Priority")
                    }
                }
            }
            .navigationBarItems(trailing: Button("Add"){
                // logic

                do {
                    try self.moc.save()
                } catch {
                    print(error.localizedDescription)
                }

                self.presentationMode.wrappedValue.dismiss()
            }.alert(isPresented: $showAlert) {
                Alert(title: Text("Name field is empty"), message: Text("Please enter a name"), dismissButton: .default(Text("Got it!")))
            })
            .navigationBarTitle("New Task")
        }
    }
}

struct AddView_Previews: PreviewProvider {
    static var previews: some View {
        AddView()
    }
}

解决方案

这是与.large navigationBarItem相关的Bug。您可以将其设置为.inline暂时解决该问题:

struct ContentView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Task.entity(),
                  sortDescriptors: [
                    NSSortDescriptor(keyPath: \Task.timestamp, ascending: true),
                    NSSortDescriptor(keyPath: \Task.status, ascending: false),
                    NSSortDescriptor(keyPath: \Task.highPriority, ascending: false),
    ]) var tasks: FetchedResults<Task>

    @State private var showingAddSheet = false
    @State private var showAlert = false
    @State private var editMode = false

    var body: some View {
        NavigationView {
            List {
                ForEach(tasks.filter{return self.filterTasks(task: $0)}, id: \.self) { task in
                    HStack {
                        TaskRowView(
                            title: task.wrappedTitle,
                            status: task.wrappedStatus,
                            timestamp: task.wrappedTimestamp,
                            highPriority: task.highPriority,
                            showDetail: self.editMode
                        ).onTapGesture {
                            self.toggleStatus(item: task)
                        }
                    }
                }
                .onDelete(perform: removeTask)

            }
            .navigationBarTitle(self.editMode ? "All Tasks" : "Today")
            .navigationBarItems(leading: Button(self.editMode ? "Done" : "Edit") {self.editMode.toggle()}, trailing: Button("Add") {self.showingAddSheet.toggle()})
            .sheet(isPresented: $showingAddSheet) {
                AddView().environment(\.managedObjectContext, self.moc)
            }
        }.onAppear(perform: {
            self.cleanupTasks()
        }).alert(isPresented: $showAlert) {
            Alert(title: Text("Unfished Task found"),
                  message: Text("Do you want to take over the old tasks or delete them?"),
                  primaryButton: .destructive(Text("Delete all")) {
                    self.removeOldTasks()
                },
                  secondaryButton: .default(Text("Take over")) {
                    self.takeOldTasksOver()
                }
            )
        }
    }

    // functions...
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        return ContentView().environment(\.managedObjectContext, context)
    }
}
#endif

相关主题:SwiftUI - Navigation bar button not clickable after sheet has been presented

2 个答案:

答案 0 :(得分:1)

当“ AddView”结构中有一个navigationView时,就会发生此问题。根据我的测试,如果您删除navigationView并仅在AddView内的其他位置使用按钮(用于解雇),则效果很好。如下:

var body: some View {
    VStack{
        HStack {
            Spacer()
            Button(action: {
                // logic ..

                self.presentationMode.wrappedValue.dismiss()

            }){
                Text("Add")
            }.alert(isPresented: $showAlert) {
                Alert(title: Text("Name field is empty"), message: Text("Please enter a name"), dismissButton: .default(Text("Got it!")))
            }
            .padding(24)
        }

        Form {
            Section(header: Text("Name")) {
                TextField("Task-Name (e.g. Eat the ?)", text: $title)
            }

            Section(header: Text("Settings")) {
                DatePicker("Date", selection: $timestamp, displayedComponents: .date)
                Toggle(isOn: $highPrio) {
                    Text("High Priority")
                }
            }
        }
    }
}

我在模拟器中遇到了这个问题,但是在实际设备上它运行良好。考虑更新xcode,mac OS或iOS。

答案 1 :(得分:0)

它正在使用最新Xcode的设备上运行。