UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I set the selection in NavigationView?

Forums > SwiftUI

Using XCode 12.5.1, Big Sure, iMac Pro.

My iOS app has a home page NavigationView which lists various entirely different pages. I want to be able to use the home NavigationView to go directly to a page, and I want to be able to swipe between the pages. I have it more or less working: this is a simplified version of the structure which shows what I'm trying to do:

struct Tab: Hashable {
    let name: String
    let id: Int
}

struct ContentView: View {

    let tabs: [Tab] = [Tab(name: "first", id: 1), Tab(name: "second", id: 2), Tab(name: "third", id: 3)]

    var body: some View {
        NavigationView {
            Form {
                List (tabs, id: \.self) { tab in
                    NavigationLink(destination: Dispatcher(which: tab.id)) {
                        VStack (alignment: .leading) {
                            Text(tab.name).font(.headline).bold()
                        }
                    }
                }
            }.navigationTitle("test")
        }
    }
}

struct Dispatcher: View {
    var which: Int
    @State private var tab = 0

    var body: some View {
        TabView(selection: $tab) {
            Text("first page").tag(1)
            Text("second page").tag(2)
            Text("third page").tag(3)
        }
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
        .onAppear(perform: { tab = which })

    }
}

On a phone or phone simulator, it works fine. On an iPad, thought, I have two problems:

  1. In the iPad Pro 12.9 inch simulator, whichever item I select in the NavigationView takes me only to the first page. On an actual iPad, though, it works fine. I suspect a simulator bug but I'd be interested if anyone doesn't have this issue.

  2. On the actual iPad Pro, I tap, say, "second" and it duly brings up the second page. I swipe left and the third page appears. However, "second" is still selected in the NavigationView list, so tapping it does nothing. How can I change the selection in the NavigationView list to match the actual displayed page?

Thanks!

Jeremy

2      

I'm wrong. I have updated my iPad to iOS 14.6 and it now behaves exactly as the simulator. Whatever i select in the NavigationView, it goes only to the first page. I can swipe to other pages but I can't go to them directly.

I don't understand!

2      

If I put

print(which, tab)

into the onAppear closure, before and after the assignment, I can see that the value of tab is changed appropriately. It's just that on the iPad, the TabView doesn't react, as it does on the iPhone.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.