gpt4 book ai didi

SwiftUI:在我关闭工作表后,navigationBarItems 中的所有按钮不再起作用

转载 作者:行者123 更新时间:2023-12-04 04:19:39 28 4
gpt4 key购买 nike

在我关闭工作表后,我上面屏幕上的按钮不再起作用。只有在按下非交互表面后,按钮才能再次工作。我使用 swift 版本 5,错误发生在模拟器和设备上。

#编辑

代码片段

AddView 这将显示在工作表中

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()
}
}

ContentView 包括一个带有一些功能的 FetchRequest,仅此而已。
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

解决方案

This is a Bug that is related to the .large navigationBarItem. You can set that to .inline to go around it for now:


NavigationView {
,,,

.navigationBarTitle(Text(""), displayMode: .inline)
}

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

最佳答案

当存在 navigationView 时会出现问题在您的“AddView”结构中。根据我的测试,如果您删除 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。

关于SwiftUI:在我关闭工作表后,navigationBarItems 中的所有按钮不再起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59696643/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com