导航链接到另一个视图中的标记

发布于 2025-01-18 09:18:38 字数 4417 浏览 3 评论 0原文

这是问题的编辑版本。我在 Swiftui 工作,我有两种观点。第一个有两个导航链接。第二个链接是我陷入困境的地方。我希望该链接转到第二页上的“tabTwo”。访问 PageTwo 不是问题...而是将 tabTwo 作为该页面上的视图访问。

这是一个编辑后的 ​​contentView 文件:

import SwiftUI

struct ContentView: View {
    
    @State var isNavigationBarHidden: Bool = true
    @State private var selectedTab = ""

    var body: some View {
        
        NavigationView {
            ZStack {
                VStack(spacing: 0) {
                    VStack(spacing: 0) {
                        NavigationLink("Link One", destination: PageTwo()).padding(10)
                            .foregroundColor(Color.gray)
                            .background(Color.white)
                        Divider().frame(width: 300,height: 1).background(Color.black)
                    }
                    VStack(spacing: 0) {
                        NavigationLink("Link Two", destination: PageTwo())
                            .padding(10)
                            .foregroundColor(Color.gray)
                            .background(Color.white)
                    }
// If you remove the below VStack everything works.
                    VStack(spacing: 0) {
                        NavigationLink("Page Two tabTwo", destination: PageTwo(), tag: "tabTwo", selection: $selectedTab?)
                            .padding(10)
                            .foregroundColor(Color.gray)
                            .background(Color.white)
                    }
                }
            }
        }
        .navigationBarTitle("Hidden Title")
        .navigationBarHidden(self.isNavigationBarHidden)
        .onAppear {self.isNavigationBarHidden = true}
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

这是一个编辑后的 ​​PageTwo:

import SwiftUI

struct PageTwo: View {
    
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>
    @State var isNavigationBarHidden: Bool = true
    @State private var selectedTab = ""

    var body: some View {
        
        ZStack() {
            TabView(selection: $selectedTab) {
                //---> Tab One
                VStack(spacing: 0) {
                    VStack{
                        Text("PAGE ONE")
                            .padding(.bottom, 20)
                    }
                    Button(action: {
                        self.mode.wrappedValue.dismiss()})
                    { Text("BACK") }
                        .font(Font.system(size:14, weight: .bold, design: .default))
                }
                .navigationBarTitle("Hidden Title")
                .navigationBarHidden(self.isNavigationBarHidden)
                .onAppear {self.isNavigationBarHidden = true}
                .tabItem {Label("ONE", systemImage: "circle.fill")}
                .tag("tabOne")
                //---> Tab Two
                VStack(spacing: 0) {
                    VStack{
                        Text("PAGE TWO")
                            .padding(.bottom, 20)
                    }
                    Button(action: {
                        self.mode.wrappedValue.dismiss()})
                    { Text("BACK") }
                        .font(Font.system(size:14, weight: .bold, design: .default))
                }
                .navigationBarTitle("Hidden Title")
                .navigationBarHidden(self.isNavigationBarHidden)
                .onAppear {self.isNavigationBarHidden = true}
                .tabItem {Label("TWO", systemImage: "circle.fill")}
                .tag("tabTwo")
            }
            .tabViewStyle(PageTabViewStyle())
            .indexViewStyle(.page(backgroundDisplayMode: .never))
            .onAppear {self.isNavigationBarHidden = true}
        }
        .navigationBarTitle("Hidden Title")
        .navigationBarHidden(self.isNavigationBarHidden)
        .onAppear {self.isNavigationBarHidden = true}
        .background(Color.gray)
        .opacity(0.75)
        .indexViewStyle(.page(backgroundDisplayMode: .never))
    }
}

struct PageTwo_Previews: PreviewProvider {
    static var previews: some View {
        PageTwo()
    }
}

这会在“某些视图”级别引发错误:

无法生成表达式的诊断;请提交错误报告 (https://swift.org/contributing/#reporting-bugs)并包括我已提交报告的项目

,但不知道如何做我想做的事情。非常感谢任何帮助。

Here's an edited version of the question. I'm working in Swiftui and I have two views. The first has two NavigationLinks. The second link is where I get stuck. I would like that link to go to 'tabTwo' on PageTwo. Getting to PageTwo isn't an issue... it's getting to tabTwo as a view on that page.

Here's an edited down contentView file:

import SwiftUI

struct ContentView: View {
    
    @State var isNavigationBarHidden: Bool = true
    @State private var selectedTab = ""

    var body: some View {
        
        NavigationView {
            ZStack {
                VStack(spacing: 0) {
                    VStack(spacing: 0) {
                        NavigationLink("Link One", destination: PageTwo()).padding(10)
                            .foregroundColor(Color.gray)
                            .background(Color.white)
                        Divider().frame(width: 300,height: 1).background(Color.black)
                    }
                    VStack(spacing: 0) {
                        NavigationLink("Link Two", destination: PageTwo())
                            .padding(10)
                            .foregroundColor(Color.gray)
                            .background(Color.white)
                    }
// If you remove the below VStack everything works.
                    VStack(spacing: 0) {
                        NavigationLink("Page Two tabTwo", destination: PageTwo(), tag: "tabTwo", selection: $selectedTab?)
                            .padding(10)
                            .foregroundColor(Color.gray)
                            .background(Color.white)
                    }
                }
            }
        }
        .navigationBarTitle("Hidden Title")
        .navigationBarHidden(self.isNavigationBarHidden)
        .onAppear {self.isNavigationBarHidden = true}
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

and here's an edited down PageTwo:

import SwiftUI

struct PageTwo: View {
    
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>
    @State var isNavigationBarHidden: Bool = true
    @State private var selectedTab = ""

    var body: some View {
        
        ZStack() {
            TabView(selection: $selectedTab) {
                //---> Tab One
                VStack(spacing: 0) {
                    VStack{
                        Text("PAGE ONE")
                            .padding(.bottom, 20)
                    }
                    Button(action: {
                        self.mode.wrappedValue.dismiss()})
                    { Text("BACK") }
                        .font(Font.system(size:14, weight: .bold, design: .default))
                }
                .navigationBarTitle("Hidden Title")
                .navigationBarHidden(self.isNavigationBarHidden)
                .onAppear {self.isNavigationBarHidden = true}
                .tabItem {Label("ONE", systemImage: "circle.fill")}
                .tag("tabOne")
                //---> Tab Two
                VStack(spacing: 0) {
                    VStack{
                        Text("PAGE TWO")
                            .padding(.bottom, 20)
                    }
                    Button(action: {
                        self.mode.wrappedValue.dismiss()})
                    { Text("BACK") }
                        .font(Font.system(size:14, weight: .bold, design: .default))
                }
                .navigationBarTitle("Hidden Title")
                .navigationBarHidden(self.isNavigationBarHidden)
                .onAppear {self.isNavigationBarHidden = true}
                .tabItem {Label("TWO", systemImage: "circle.fill")}
                .tag("tabTwo")
            }
            .tabViewStyle(PageTabViewStyle())
            .indexViewStyle(.page(backgroundDisplayMode: .never))
            .onAppear {self.isNavigationBarHidden = true}
        }
        .navigationBarTitle("Hidden Title")
        .navigationBarHidden(self.isNavigationBarHidden)
        .onAppear {self.isNavigationBarHidden = true}
        .background(Color.gray)
        .opacity(0.75)
        .indexViewStyle(.page(backgroundDisplayMode: .never))
    }
}

struct PageTwo_Previews: PreviewProvider {
    static var previews: some View {
        PageTwo()
    }
}

This throws an error at the 'some view' level of:

Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project

I've submitted the report, but can't figure out how to do what I'd like. Any help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文