1 year ago

#365605

test-img

monkeyBoy

navigationLink to tag in another view

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.

swift

tags

swiftui-navigationlink

swiftui-navigationview

swiftui-tabview

0 Answers

Your Answer

Accepted video resources