Swift + macOS + Xcode : Show list item in navigation pane as selected programatically


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.



Please use the proper tags when you create a post. This should be in the "Using Swift" category. This has nothing to do with the compiler or development of the Swift language more generally. Also, don't post images of your code; post it as text so that people can copy it.

What you want is to make the list in the sidebar the first responder. The first responder is the object that responds to user events—such as key events—first. This may be possible in the beta version of SwiftUI bundled with Xcode 13 and which requires macOS 12. I haven't been able to test this out because I'm weary of installing a beta version of macOS.

Terms of Service

Privacy Policy

Cookie Policy