[英]SwiftUI - multiple levels deep NavigationLink does not work
So I thought I found a way to make navigation in SwiftUI flexible and loosely coupled, yet still state-based and somewhat free of imperative-navigation bugs (double push, etc).所以我想我找到了一种方法来使 SwiftUI 中的导航灵活且松散耦合,但仍然基于状态并且在某种程度上没有命令式导航错误(双推等)。
Basic idea is to have a linked list of Views (erased to AnyView) and a recursive view with NavigationLink in it, which is active when corresponding view is present in the list基本思想是有一个 Views 的链接列表(擦除到 AnyView)和一个带有 NavigationLink 的递归视图,当列表中存在相应的视图时它是活动的
But it does not work and I don't understand why.但它不起作用,我不明白为什么。 On iOS device it only pushes one level deep, even though the list is multiple levels deep and the bindings return true
在 iOS 设备上,它只推送一层深度,即使列表是多层深度并且绑定返回 true
Is it a SwiftUI bug or am I missing something?是 SwiftUI 错误还是我遗漏了什么?
struct ContentView: View {
@State
var navigationList: NavigationList?
var body: some View {
NavigationView {
Navigatable(list: $navigationList) {
Button("Push test", action: {
navigationList = .init(next: nil, screen: Screen {
TestView()
})
})
}
}
}
}
struct TestView: View {
@Environment(\.navigationList)
@Binding
var list
var body: some View {
Button("Push me", action: {
list = .init(next: nil, screen: Screen {
TestView()
})
})
}
}
struct Navigatable<Content: View>: View {
@Binding
var list: NavigationList?
let content: () -> Content
init(list: Binding<NavigationList?>, @ViewBuilder content: @escaping () -> Content) {
self._list = list
self.content = content
}
var body: some View {
ZStack {
NavigationLink(isActive: isActive, destination: {
Navigatable<Screen?>(list: childBinding) {
list?.screen
}
}, label: EmptyView.init).hidden()
LazyView {
content()
}.environment(\.navigationList, $list)
}
}
var isActive: Binding<Bool> {
.init(
get: { list != nil },
set: {
if !$0 {
list = nil
}
}
)
}
var childBinding: Binding<NavigationList?> {
.init(
get: { list?.next },
set: { list?.next = $0 }
)
}
}
struct Screen: View {
let content: () -> AnyView
init<C: View>(@ViewBuilder content: @escaping () -> C) {
self.content = {
.init(content())
}
}
var body: some View {
content()
}
}
struct NavigationList {
@Indirect
var next: NavigationList?
let screen: Screen
}
enum NavigationListKey: EnvironmentKey {
static var defaultValue: Binding<NavigationList?> {
.constant(nil)
}
}
extension EnvironmentValues {
var navigationList: Binding<NavigationList?> {
get { self[NavigationListKey.self] }
set { self[NavigationListKey.self] = newValue }
}
}
struct LazyView<Content: View>: View {
@ViewBuilder var content: () -> Content
var body: some View {
content()
}
}
@propertyWrapper
struct Indirect<Wrapped> {
private final class Storage: CustomReflectable {
var wrapped: Wrapped
init(_ wrapped: Wrapped) {
self.wrapped = wrapped
}
var customMirror: Mirror {
.init(self, children: [(label: "wrapped", value: wrapped)])
}
}
private let storage: Storage
var wrappedValue: Wrapped {
get { storage.wrapped }
mutating set { storage.wrapped = newValue }
}
init(wrappedValue: Wrapped) {
self.storage = .init(wrappedValue)
}
}
You're missing 'isDetailLink(false)'你错过了'isDetailLink(false)'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.