gpt4 book ai didi

ios - SwiftUI:检测 NavigationView 后退按钮按下

转载 作者:行者123 更新时间:2023-12-05 00:23:09 24 4
gpt4 key购买 nike

SwiftUI当我在 DetailView1 内时,我找不到检测用户何时点击导航 View 的默认后退按钮的方法。在这段代码中:

struct RootView: View {
@State private var showDetails: Bool = false
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView1(), isActive: $showDetails) {
Text("show DetailView1")
}
}
.navigationBarTitle("RootView")
}
}
}

struct DetailView1: View {
@State private var showDetails: Bool = false
var body: some View {
NavigationLink(destination: DetailView2(), isActive: $showDetails) {
Text("show DetailView2")
}
.navigationBarTitle("DetailView1")
}
}

struct DetailView2: View {
var body: some View {
Text("")
.navigationBarTitle("DetailView2")
}
}


使用 .onDisappear不能解决问题,因为在弹出 View 或推送新 View 时会调用它的闭包。

最佳答案

快速的解决方案是创建一个自定义的后退按钮,因为现在框架没有这种可能性。

struct DetailView : View {

@Environment(\.presentationMode) var mode: Binding<PresentationMode>

var body : some View {
Text("Detail View")
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: Button(action : {
self.mode.wrappedValue.dismiss()
}){
Image(systemName: "arrow.left")
})
}
}

关于ios - SwiftUI:检测 NavigationView 后退按钮按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61930915/

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