我在为一些看起来很简单的事情而苦苦挣扎. 我有一个带有导航链接的视图,因为它不是我的应用程序的主要视图.在此视图中,我有一些发送到此视图的链接,但具有不同的数据.我想嵌套这些链接,但现在我在顶部收到了双选项卡视图.我怎样才能改变这一点?我知道我需要切换第二个视图上的导航栏,但我的第二个视图实际上是同一个视图,但具有不同的数据.

以下是代码

/// ContentView
struct MatchListView: View {
   var namespace: Namespace.ID
   @ObservedObject var dataViewModel = DataViewModel()
   @State private var selectedMatchID: Int?
   @State private var selectedDayIndex: Int = 0
   @State private var headerHeight: CGFloat = 0
   @State private var didFinishFirst = false

   var body: some View {
       ForEach(data.leagues, id: \.id) { league in
           VStack {
               leagueHeader(league: league)
               ForEach(league.matches, id: \.id) { match in
                   matchRow(match: match, namespace: namespace)
               }
            }
           .foregroundStyle(Color.textAccent)
           .background(Color.bgSecondaryAccent)
           .cornerRadius(10)
       }
   }
}

func matchRow(match: Match, namespace: Namespace.ID) -> some View {
   NavigationLink(destination: MatchDetailsView(namespace: namespace, matchID: match.id, homeTeamName: match.home.name, awayTeamName: match.away.name)) { 
       Text("go to match \(match.id)")
   }
}


/// MatchDetails View
struct MatchDetailsView: View {
   NavigationView {  
       List {
           ScrollView(showsIndicators: false) {
               HighlightsView(matchViewModel: matchViewModel)
               MOTMView(matchViewModel: matchViewModel)
               EventsView(matchViewModel: matchViewModel)
               MatchStats(matchViewModel: matchViewModel)
               MatchTopPlayers(matchViewModel: matchViewModel)                       
               TeamFormView(matchViewModel: matchViewModel)
           }        
       }
   }
   .toolbar {
       ShareLink(item: URL(string: "https://maykmayk.github.io/Balun/#/match-details/\(matchID)")!) {
           Label("", systemImage: "square.and.arrow.up")
       }
       Button {
           // action
       } label: {
           Image(systemName: "bell")
       }
       Button {
           // action
        } label: {
           Image(systemName: "star")
       }
   }
   .toolbarRole(.editor)
   .tint(Color(.textAccent))
}


/// TeamformView
struct TeamFormView: View {

   var body: some View {
       NavigationLink(destination: MatchDetailsView(namespace: namespace, matchID: getMatchID(from: match.linkToMatch ?? ""), homeTeamName: match.home?.name ?? "", awayTeamName: match.away?.name ?? "")) {
           Text(match.score ?? "")
               .foregroundColor(.white)
               .font(.system(size: 14, weight: .medium))
               .padding(.horizontal, 5)
               .padding(.vertical, 2.5)
               .background(
                   match.resultString == "W" ? Color.greenAccent :
                   match.resultString == "L" ? Color.redAccent :
                   match.resultString == "D" ? Color.gray.opacity(0.5) :
                   Color.black
               )
               .clipShape(RoundedRectangle(cornerRadius: 6, style: .continuous))
       }
   }
}

我的预览: https://ibb.co/gRHcDSb个 我想要的是: https://ibb.co/W6ysFYn

推荐答案

在这里将其发布为Answare. 您面临的问题是由于嵌套的NavigationView造成的.这就是为什么您在导航到新屏幕时会看到多个导航栏. Tip:除非您支持iOS 15或更早版本,否则请考虑使用NavigationStack.

您可以编辑您的代码,如下所示:

NavigationView {
    MatchListView(namespace: namespace)
}

我想MatchListView可以保持原样了:

struct MatchListView: View {
   var namespace: Namespace.ID
   //@ObservedObject var dataViewModel = DataViewModel()
   @State private var selectedMatchID: Int?
   @State private var selectedDayIndex: Int = 0
   @State private var headerHeight: CGFloat = 0
   @State private var didFinishFirst = false

   var body: some View {
       ForEach(["Leage 1", "League 2"], id: \.self) { league in
           VStack {
               
               ForEach(["Match 1", "Match 2"], id: \.self) { match in
                   NavigationLink(destination: MatchDetailsView()) {
                          Text("go to match ID")
                    }
               }
            }
           .foregroundStyle(.black)
           .background(.fill)
           .cornerRadius(10)
       }
   }
}

从此处删除其他导航视图:

struct MatchDetailsView: View {
    
    var body: some View {
        
        /// Remove this NavigationView and others if parents views already have one
        //NavigationView {
            List {
                ScrollView(showsIndicators: false) {
                    //               HighlightsView(matchViewModel: matchViewModel)
                    //               MOTMView(matchViewModel: matchViewModel)
                    //               EventsView(matchViewModel: matchViewModel)
                    //               MatchStats(matchViewModel: matchViewModel)
                    //               MatchTopPlayers(matchViewModel: matchViewModel)
                    TeamFormView()
                }
            }
            
            .toolbar {
                
            }
            .toolbarRole(.editor)
            .tint(.accentColor)
        //}
    }
    
}

我希望这对你很有效,而且解释很清楚!

Swift相关问答推荐

日期格式yyyyyy—MM—dd hh:mm:ss +0000到dd MMM,以swift格式表示""""

如何在SWIFT中使用DiscardingTaskGroup获得任务结果

如何在自定义视图中读取修改符子元素?

将NavigationSplitView更改为NavigationStack

SwiftData/PersistentModel.swft:540:致命错误:不支持的关系密钥路径ReferenceWritableKeyPath

如何将变量添加到不属于可解码部分的 struct ?

Swift 中的Combine和didSet有什么区别?

有什么方法可以快速为泛型参数分配默认值?

是否可以使任何协议符合 Codable?

将数据附加到 Firebase 实时数据库后,NavigationLink 将我重定向到上一个视图

如何在 SwiftUI 的 fileImporter 中为 allowedContentTypes 设置 xcodeproj 类型?

.onTapGesture 不适用于 Stepper,仅适用于其文本

AVPlayer 在 iOS 15.4 中寻求 completionHandler 返回 false

在 Swift 中返回实例类型

如何轻松删除领域中的所有对象

UITableViewAlertForLayoutOutsideViewHierarchy 错误:仅警告一次(iOS 13 GM)

如何使用 swift 在 tableview 中填充两个不同数组的两个部分?

在 Swift 中子类化 NSObject - 初始化器的最佳实践

'类 'className' 的 NSManagedObject 必须具有有效的 NSEntityDescription.错误

在情节提要中设置的 UITableViewCell 的恢复 ID 和标识符有什么区别