SwiftUI NavigationView 协调器问题
SwiftUI NavigationView Coordinator Issue
我举了一个例子来演示我是如何尝试从视图中排除导航状态的。问题在于以某种方式将导航状态存储在 ObservableObject 中会破坏导航。点击“Show SecondDetailView”NavigationLink 后,SecondDetailView 会显示很短的时间,然后视图就会弹出。
有人可以解释为什么无法导航到 SecondDetailView 吗?
class NavigationCoordinator: ObservableObject {
@Published var showFirstDetailView: Bool = false
@Published var showSecondDetailView: Bool = false
@Published var showThirdDetailView: Bool = false
}
struct MainView: View {
@StateObject
var navigationCoordinator: NavigationCoordinator = NavigationCoordinator()
var body: some View {
NavigationView {
VStack {
Text("MainView").font(.headline)
Text("showFirstDetailView: \(navigationCoordinator.showFirstDetailView.description)")
NavigationLink(isActive: $navigationCoordinator.showFirstDetailView, destination: {FirstDetailView()}, label: {Text("Show FirstDetailView")})
}
}
.environmentObject(navigationCoordinator)
}
}
struct FirstDetailView: View {
@EnvironmentObject
var navigationCoordinator: NavigationCoordinator
var body: some View {
VStack {
Text("FirstDetailView").font(.headline)
Text("showSecondDetailView: \(navigationCoordinator.showSecondDetailView.description)")
NavigationLink(isActive: $navigationCoordinator.showSecondDetailView, destination: {SecondDetailView()}, label: {Text("Show SecondDetailView")})
}
}
}
struct SecondDetailView: View {
@EnvironmentObject
var navigationCoordinator: NavigationCoordinator
var body: some View {
VStack {
Text("SecondDetailView").font(.headline)
Text("showThirdDetailView: \(navigationCoordinator.showThirdDetailView.description)")
NavigationLink(isActive: $navigationCoordinator.showThirdDetailView, destination: {ThirdDetailView()}, label: {Text("Show ThirdDetailView")})
}
}
}
struct ThirdDetailView: View {
@EnvironmentObject
var navigationCoordinator: NavigationCoordinator
var body: some View {
Text("ThirdDetailView").font(.headline)
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
}
}
不知道为什么 SecondDetailView
不起作用,内部代码对我们不可用。但要“修复”问题,只需将 .navigationViewStyle(.stack)
添加到 NavigationView
。适合我。
我举了一个例子来演示我是如何尝试从视图中排除导航状态的。问题在于以某种方式将导航状态存储在 ObservableObject 中会破坏导航。点击“Show SecondDetailView”NavigationLink 后,SecondDetailView 会显示很短的时间,然后视图就会弹出。
有人可以解释为什么无法导航到 SecondDetailView 吗?
class NavigationCoordinator: ObservableObject {
@Published var showFirstDetailView: Bool = false
@Published var showSecondDetailView: Bool = false
@Published var showThirdDetailView: Bool = false
}
struct MainView: View {
@StateObject
var navigationCoordinator: NavigationCoordinator = NavigationCoordinator()
var body: some View {
NavigationView {
VStack {
Text("MainView").font(.headline)
Text("showFirstDetailView: \(navigationCoordinator.showFirstDetailView.description)")
NavigationLink(isActive: $navigationCoordinator.showFirstDetailView, destination: {FirstDetailView()}, label: {Text("Show FirstDetailView")})
}
}
.environmentObject(navigationCoordinator)
}
}
struct FirstDetailView: View {
@EnvironmentObject
var navigationCoordinator: NavigationCoordinator
var body: some View {
VStack {
Text("FirstDetailView").font(.headline)
Text("showSecondDetailView: \(navigationCoordinator.showSecondDetailView.description)")
NavigationLink(isActive: $navigationCoordinator.showSecondDetailView, destination: {SecondDetailView()}, label: {Text("Show SecondDetailView")})
}
}
}
struct SecondDetailView: View {
@EnvironmentObject
var navigationCoordinator: NavigationCoordinator
var body: some View {
VStack {
Text("SecondDetailView").font(.headline)
Text("showThirdDetailView: \(navigationCoordinator.showThirdDetailView.description)")
NavigationLink(isActive: $navigationCoordinator.showThirdDetailView, destination: {ThirdDetailView()}, label: {Text("Show ThirdDetailView")})
}
}
}
struct ThirdDetailView: View {
@EnvironmentObject
var navigationCoordinator: NavigationCoordinator
var body: some View {
Text("ThirdDetailView").font(.headline)
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
}
}
不知道为什么 SecondDetailView
不起作用,内部代码对我们不可用。但要“修复”问题,只需将 .navigationViewStyle(.stack)
添加到 NavigationView
。适合我。