How to use SwiftUI NavigationView
? Here is the basic strategy.
import SwiftUI struct MasterView: View { // Model of the view is an array of `Int` let xs = [0, 1, 2, 3, 4, 5] var body: some View { NavigationView { List { ForEach(xs, id: \.self) { x in // Pass selected row's data to `DetailView` NavigationLink(destination: DetailView(x: x)) { // Convert number to the list row Text(String(x)) .padding() } } } .listStyle(.plain) .navigationBarTitleDisplayMode(.inline) // classic title bar style .navigationTitle("Master View") // title bar text .toolbar { // Left button ToolbarItemGroup(placement: .navigationBarLeading) { Button { print("Leading") } label: { Text("Leading") } } // Right button ToolbarItemGroup(placement: .navigationBarTrailing) { Button { print("Trailing") } label: { Label("Trailing", systemImage: "plus") } } } } } }
And this is what DetailView
looks like:
import SwiftUI struct DetailView: View { let x: Int var body: some View { // Convert the number passed from the `MasterView` to string and show it in the title bar and in the centre of the screen let s = String(x) Text(s) .font(.title) .navigationTitle("Detail \(s)") } }