This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct ContentView: View { | |
| let customers = ["Alex", "Mary", "John"] | |
| @State var selectedCustomer: String? | |
| var body: some View { | |
| NavigationView { | |
| VStack { | |
| // get the selected customer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct CustomerListView: View { | |
| let customers: [String] | |
| let onSelected: ((String) -> Void)? | |
| init(customers: [String], onSelected: ((String) -> Void)? = nil) { | |
| self.customers = customers | |
| self.onSelected = onSelected | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var body: some View { | |
| NavigationView { | |
| VStack { | |
| CustomerListView(customers: customers) | |
| } | |
| .navigationTitle("Customers") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct CustomerListView: View { | |
| let customers: [String] | |
| var body: some View { | |
| List(customers, id: \.self) { customer in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NavigationView { | |
| VStack { | |
| List(customers, id: \.self) { customer in | |
| NavigationLink( | |
| destination: DetailView(customer: customer), | |
| label: { | |
| HStack { | |
| Text(customer) | |
| Spacer() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct DetailView: View { | |
| let customer: String | |
| var body: some View { | |
| Text(customer) | |
| .font(.largeTitle) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ContentView.swift | |
| // RoutingDemo | |
| // | |
| // Created by Mohammad Azam on 1/13/21. | |
| // | |
| import SwiftUI | |
| extension View { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NavigationView { | |
| VStack { | |
| Button(action: {}, label: { | |
| Navigator.navigate(.detail("USA"), content: { | |
| Text("GO") | |
| }) | |
| }) | |
| .navigationTitle("Countries") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NavigationView { | |
| VStack { | |
| List(countries, id: \.self) { country in | |
| Navigator.navigate(.detail(country), content: { | |
| Text(country) | |
| }) | |
| }.listStyle(PlainListStyle()) | |
| .navigationTitle("Countries") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Navigator { | |
| static func navigate<T: View>(_ route: Route, content: () -> T) -> AnyView { | |
| switch route { | |
| case .detail(let country): | |
| return | |
| NavigationLink( | |
| destination: DetailView(country: country)) { | |
| content() |