SwipeActions 按钮和 ToolBar 按钮触发的多个 sheet(item: ) 在 SwiftUI 中第一次获取 nil 对象
Multiple sheet(item: ) triggered by SwipeActions button and ToolBar buttons gets nil object in the first time in SwiftUI
我在列表和工具栏按钮的 ForEach 循环中使用 swipeActions 以在我的 SwiftUI 视图上显示不同的 sheet。但是每次我第一次向左滑动并单击“编辑”按钮时,该行的对象为零。如果我进行相同的滑动并再次单击,一切都会顺利进行。以前有其他人遇到过这种错误吗?谢谢。
相关代码如下:
结构语言视图:视图{
@State var activeSheet: ActiveSheet?
@State var toBeEdit: MeLanguage?
var body: some View {
NavigationView {
List {
ForEach(self.meLanguages, id: \.id) { lan in
HStack {
Text("\(lan.wrappedName)")
.font(.headline)
}.swipeActions(allowsFullSwipe: false) {
Button(
action: {
self.activeSheet = .editLanguage
self.toBeEdit = lan
},
label: { Label("Edit", systemImage: "pencil") }
) .tint(.indigo)
}
}
}
.sheet(item: $activeSheet,
onDismiss: {
self.toBeEdit = nil
}
){
item in
switch item {
case .addLanguage:
AddLanguage()
case .sortLanguages:
SortLanguagesView()
case .editLanguage:
if self.toBeEdit != nil {
EditLanguageView( meLanguage: self.toBeEdit! )
}
else {
Text("self.toBeEdit is nil")
}
default:
Text("No such button on ContentView.")
}
}
.toolbar {
ToolbarItemGroup {
HStack {
Text("\(self.meLanguages.count) Languages on Card").font(.headline)
self.barButtons
}
}
}
}
}
var barButtons: some View {
HStack {
Button(
action: {
self.activeSheet = .sortLanguages
},
label: { Label("Sort Languages", systemImage: "arrow.up.arrow.down.circle")
}
).id("sortLanguages")
Button(
action: {
self.activeSheet = .addLanguage
},
label: { Label("Add Language",
systemImage: "plus")
.imageScale(.large)
}
)
}
}
}
如果我只想到 sheet 由 swipeActions Edit 按钮触发,下面的代码可以完美运行。但我仍然需要其他 sheet 由工具栏按钮触发。
.sheet(item: self.$toBeEdit, content: { toBeEdit in
EditLanguageView( meLanguage: toBeEdit)
})
经过更多搜索,我意识到这与 SwipeActions 无关。这实际上类似于这个问题:
SwiftUI presenting sheet with Binding variable doesn't work when first presented
所以我在ForEach循环后添加了一个隐藏的Text,问题就解决了。
ForEach(self.meLanguages, id: \.id) { ... }
if self.toBeEdit != nil {
Text("\(self.toBeEdit!.wrappedName)").hidden()
}
我在列表和工具栏按钮的 ForEach 循环中使用 swipeActions 以在我的 SwiftUI 视图上显示不同的 sheet。但是每次我第一次向左滑动并单击“编辑”按钮时,该行的对象为零。如果我进行相同的滑动并再次单击,一切都会顺利进行。以前有其他人遇到过这种错误吗?谢谢。
相关代码如下:
结构语言视图:视图{
@State var activeSheet: ActiveSheet?
@State var toBeEdit: MeLanguage?
var body: some View {
NavigationView {
List {
ForEach(self.meLanguages, id: \.id) { lan in
HStack {
Text("\(lan.wrappedName)")
.font(.headline)
}.swipeActions(allowsFullSwipe: false) {
Button(
action: {
self.activeSheet = .editLanguage
self.toBeEdit = lan
},
label: { Label("Edit", systemImage: "pencil") }
) .tint(.indigo)
}
}
}
.sheet(item: $activeSheet,
onDismiss: {
self.toBeEdit = nil
}
){
item in
switch item {
case .addLanguage:
AddLanguage()
case .sortLanguages:
SortLanguagesView()
case .editLanguage:
if self.toBeEdit != nil {
EditLanguageView( meLanguage: self.toBeEdit! )
}
else {
Text("self.toBeEdit is nil")
}
default:
Text("No such button on ContentView.")
}
}
.toolbar {
ToolbarItemGroup {
HStack {
Text("\(self.meLanguages.count) Languages on Card").font(.headline)
self.barButtons
}
}
}
}
}
var barButtons: some View {
HStack {
Button(
action: {
self.activeSheet = .sortLanguages
},
label: { Label("Sort Languages", systemImage: "arrow.up.arrow.down.circle")
}
).id("sortLanguages")
Button(
action: {
self.activeSheet = .addLanguage
},
label: { Label("Add Language",
systemImage: "plus")
.imageScale(.large)
}
)
}
}
}
如果我只想到 sheet 由 swipeActions Edit 按钮触发,下面的代码可以完美运行。但我仍然需要其他 sheet 由工具栏按钮触发。
.sheet(item: self.$toBeEdit, content: { toBeEdit in
EditLanguageView( meLanguage: toBeEdit)
})
经过更多搜索,我意识到这与 SwipeActions 无关。这实际上类似于这个问题:
SwiftUI presenting sheet with Binding variable doesn't work when first presented
所以我在ForEach循环后添加了一个隐藏的Text,问题就解决了。
ForEach(self.meLanguages, id: \.id) { ... }
if self.toBeEdit != nil {
Text("\(self.toBeEdit!.wrappedName)").hidden()
}