[英]How can I keep a list of View structs in SwiftUI?
Since SwiftUI's NavigationView
is extremely rigid and does not provide an easy way for programatic navigation, I wanted to create an AppNavigationView
that has an array of views and updates its rendered view based on push/pop.由于 SwiftUI 的
NavigationView
非常死板,并且没有为编程导航提供简单的方法,因此我想创建一个AppNavigationView
,它具有一组视图并根据推送/弹出更新其渲染视图。
Because a SwiftUI view is a protocol with an associatedType, I cannot either create an array of Views or even pass a few in my public func pushView(view:View)
.因为 SwiftUI 视图是具有关联类型的协议,所以我无法创建视图数组,甚至无法在我的
public func pushView(view:View)
中传递一些视图。
So how can I store a list of View structs?那么如何存储 View 结构的列表呢?
// Error: Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements.
@State var navigationStack: [View]
Swift does not allow to use protocol as member type, so most close would be to use Swift 不允许使用协议作为成员类型,所以最接近的是使用
@State private var navigationStack: [AnyView]
//...
public func pushView<V:View>(view: V) {
navigationStack.append(AnyView(view))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.