【发布时间】:2020-05-05 04:10:27
【问题描述】:
当用户点击后退按钮时,我们可以获取事件并做一些事情吗?
我已尝试使用onDisappear 进行查看,但不希望出现孩子消失和父母出现的顺序。所以我正在寻找一种方法来挂钩 back 事件。
【问题讨论】:
标签: ios swiftui navigationview
当用户点击后退按钮时,我们可以获取事件并做一些事情吗?
我已尝试使用onDisappear 进行查看,但不希望出现孩子消失和父母出现的顺序。所以我正在寻找一种方法来挂钩 back 事件。
【问题讨论】:
标签: ios swiftui navigationview
你可以使用修饰符onDisappear,我不明白你的具体情况,因为你需要返回的确切事件。
struct ContentView: View {
@State var isPresented: Bool = false
var destination: some View {
Text("Detail")
.onDisappear(perform: {
print("On Disappear")
})
}
var body: some View {
NavigationView {
NavigationLink(destination: destination, isActive: $isPresented) {
Text("Main")
}
}
}
}
【讨论】:
我认为你可以使用 isActive 并监听绑定的变化,我知道绑定没有didSet 但你可以用这个扩展添加它
extension Binding {
func didSet(execute: @escaping (Value) ->Void) -> Binding {
return Binding(
get: {
return self.wrappedValue
},
set: {
execute($0)
self.wrappedValue = $0
}
)
}
}
然后可以添加状态来监控视图的状态
struct ContentView: View {
@State isPresented: Bool = false
var body: some View {
NavigationView {
NavigationLink(Text("Follow to a Text"), destination: Text("detail view"), isPresented: $isPresented.didSet{ if !self.isPresented { print("Back Pressed") } })
}
}
目前我不知道没有自定义按钮的任何其他方式来做你想做的事
【讨论】: