SwiftUI + macOS + Xcode : Show list item in navigation pane as selected programatically at program start

Hello,

I have a SwiftUI program that displays one of three views based on a selection from a list in a Navigation Pane. I am able to display an initial view programatically. At this point there is a border around the initialized selection in the Navigation Pane but it is dark grey. Once selected with the mouse the border turns blue and now I can move up and down the list with the arrow keys on the keyboard. Is there a way to programatically perform what ever the mouse is doing to change the background color to blue and thus allow me to move up and down the list with the arrow keys?

Below is the code.

Regards,

Chris

@ViewBuilder
func fetchView(item : Int) -> some View {

switch item {
case 1: FirstView()
case 2: SecondView()
case 3: ThirdView()
default:
    FirstView()
}

}

struct ContentView: View {

@State private var initialView: Int? = 1
var theViewNames : [String] = ["First View","Second View", "Third View"]

var body: some View {
    VStack{
        NavigationView {
            VStack{
                Text ("Select View").bold()
                    .padding()
                Spacer()
                    .frame(height: 20)
                List (1..<4) {item in
                    NavigationLink (destination: fetchView(item: item), tag: item, selection: self.$initialView ) {Text (theViewNames[item - 1])}
                }
                Spacer()
          }
        }
    }.frame(width: 900, height: 600, alignment: .center)
    
}

}

Terms of Service

Privacy Policy

Cookie Policy