SwiftUI navigationLink isActivie 不工作
SwiftUI navigationLink isActivie not working
目的
我的意图是从视图 3 返回并 return 直接返回到视图 1
当前状态
我这里的视图层次结构是view1 -> view2 -> view3。
我在view1中用NavigationLink打开view2,用Binding传一个值给isActivie,再把这个值传给view3,当view3完成后,把传的值改成false,自动关闭view3。
但结果是它停留在view2中。那么传入的值等于false
背景和代码
// view 1
@State private var detailViewIsShow: Bool = false
var body some: View {
ForEach(boughtItems) { item in
NavigationLink(
destination: ItemDetailView(item: item, detailViewIsShow: $detailViewIsShow),
isActive: $detailViewIsShow
) {
ItemCellView(item: item)
}
.isDetailLink(false)
}
}
// view 2
@Binding var detailViewIsShow: Bool
@State var itemEditorViewIsShow: Bool = false
var body: some View {
ZStack(alignment: .bottom) {
NavigationLink(
destination: ItemEditorView(item: item, detailViewIsShow: $detailViewIsShow),
isActive: self.$itemEditorViewIsShow
) {
EmptyView()
}
.isDetailLink(false)
}
.padding(.horizontal, 16)
.navigationBarTitleDisplayMode(.inline)
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
self.itemEditorViewIsShow = true
}, label: {
Image(systemName: "square.and.pencil")
.frame(width: 24, height: 24)
})
}
}
}
// view 3
@Binding var detailViewIsShow: Bool
struct body: some View {
someView()
.confirmationDialog("", isPresented: $isShowDeleteAlert) {
Button("delete", role: .destructive) {
item!.delete(context: context)
self.detailViewIsShow = false
presentationMode.wrappedValue.dismiss()
}
}
}
您可以在“detailViewIsShow”的视图 2 中添加 onChange() 侦听器 属性,当它为 false 时,关闭当前视图
.onChange(of: detailViewIsShow) { newValue in
if !newValue{
presentationMode.wrappedValue.dismiss()
}
}
目的
我的意图是从视图 3 返回并 return 直接返回到视图 1
当前状态
我这里的视图层次结构是view1 -> view2 -> view3。
我在view1中用NavigationLink打开view2,用Binding传一个值给isActivie,再把这个值传给view3,当view3完成后,把传的值改成false,自动关闭view3。
但结果是它停留在view2中。那么传入的值等于false
背景和代码
// view 1
@State private var detailViewIsShow: Bool = false
var body some: View {
ForEach(boughtItems) { item in
NavigationLink(
destination: ItemDetailView(item: item, detailViewIsShow: $detailViewIsShow),
isActive: $detailViewIsShow
) {
ItemCellView(item: item)
}
.isDetailLink(false)
}
}
// view 2
@Binding var detailViewIsShow: Bool
@State var itemEditorViewIsShow: Bool = false
var body: some View {
ZStack(alignment: .bottom) {
NavigationLink(
destination: ItemEditorView(item: item, detailViewIsShow: $detailViewIsShow),
isActive: self.$itemEditorViewIsShow
) {
EmptyView()
}
.isDetailLink(false)
}
.padding(.horizontal, 16)
.navigationBarTitleDisplayMode(.inline)
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
self.itemEditorViewIsShow = true
}, label: {
Image(systemName: "square.and.pencil")
.frame(width: 24, height: 24)
})
}
}
}
// view 3
@Binding var detailViewIsShow: Bool
struct body: some View {
someView()
.confirmationDialog("", isPresented: $isShowDeleteAlert) {
Button("delete", role: .destructive) {
item!.delete(context: context)
self.detailViewIsShow = false
presentationMode.wrappedValue.dismiss()
}
}
}
您可以在“detailViewIsShow”的视图 2 中添加 onChange() 侦听器 属性,当它为 false 时,关闭当前视图
.onChange(of: detailViewIsShow) { newValue in
if !newValue{
presentationMode.wrappedValue.dismiss()
}
}