gpt4 book ai didi

ios - 只有根级导航目的地对具有同构路径的导航堆栈有效

转载 作者:行者123 更新时间:2023-12-02 18:01:15 24 4
gpt4 key购买 nike

我正在尝试将 NavigationStack 集成到我的 SwiftUI 应用中。

我有四个 View :CealUIAppOnBoardingViewUserTypeViewRegisterView

当用户在 OnBoardingView 中按下按钮时,我想从 OnBoardingView 导航到 UserTypeView

并且,当用户按下 UserTypeView 中的按钮时,从 UserTypeView 导航到 RegisterView

下面是我的 CealUIApp 代码

@main
struct CealUIApp: App {

@State private var path = [String]()

var body: some Scene {
WindowGroup {
NavigationStack(path: $path){
OnBoardingView(path: $path)
}
}
}
}

OnBoardingView

Button {
path.append("UserTypeView")
}
label: {
Text("Hello")
}
.navigationDestination(for: String.self) { string in
UserTypeView(path: $path)
}

UserTypeView

Button {
path.append("RegisterView")
}
label: {
Text("Hello")
}
.navigationDestination(for: String.self) { string in
RegisterView()
}

当按下 UserTypeView 上的按钮时,它会导航到 UserTypeView 而不是 RegisterView

此外,Xcode 日志说只有根级导航目的地对具有同构路径的导航堆栈有效。

最佳答案

您可以通过将路径类型更改为 NavigationPath 来摆脱 Only root-level navigation destinations is effective for a navigation stack with a homogeneous path

@State private var path: NavigationPath = .init()

但随后您会收到一条消息/错误,我认为它可以更好地解释问题 “Swift.String”的导航目标已在堆栈中较早地声明。只会使用声明为最接近堆栈 Root View 的目标。

Apple 决定扫描所有可用的 View 是非常低效的,因此他们将优先使用 navigationDestination

试想一下,如果您的 OnBoardingView 也有一个 "RegisterView" 选项

 .navigationDestination(for: String.self) { string in
switch string{
case "UserTypeView":
UserTypeView(path: $path)
case "RegisterView":
Text("fakie register view")
default:
Text("No view has been set for \(string)")
}

}

SwiftUI 如何选择合适的?

那么如何“修复”呢?您可以试试这个替代方案。

import SwiftUI

@available(iOS 16.0, *)
struct CealUIApp: View {
@State private var path: NavigationPath = .init()
var body: some View {
NavigationStack(path: $path){
OnBoardingView(path: $path)
.navigationDestination(for: ViewOptions.self) { option in
option.view($path)
}
}
}
//Create an `enum` so you can define your options
enum ViewOptions{
case userTypeView
case register
//Assign each case with a `View`
@ViewBuilder func view(_ path: Binding<NavigationPath>) -> some View{
switch self{
case .userTypeView:
UserTypeView(path: path)
case .register:
RegisterView()
}
}
}
}
@available(iOS 16.0, *)
struct OnBoardingView: View {
@Binding var path: NavigationPath
var body: some View {
Button {
//Append to the path the enum value
path.append(CealUIApp.ViewOptions.userTypeView)
} label: {
Text("Hello")
}

}
}
@available(iOS 16.0, *)
struct UserTypeView: View {
@Binding var path: NavigationPath
var body: some View {
Button {
//Append to the path the enum value
path.append(CealUIApp.ViewOptions.register)
} label: {
Text("Hello")
}

}
}
@available(iOS 16.0, *)
struct RegisterView: View {
var body: some View {
Text("Register")

}
}
@available(iOS 16.0, *)
struct CealUIApp_Previews: PreviewProvider {
static var previews: some View {
CealUIApp()
}
}

关于ios - 只有根级导航目的地对具有同构路径的导航堆栈有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74362455/

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