6
6
// The link to the code is under the video
7
7
//
8
8
9
-
10
9
import SwiftUI
11
10
12
- //experimenting with nav link
11
+ // experimenting with nav link
13
12
struct ContentView : View {
14
-
15
- @State var route : Router = . empty
13
+ @State var route : Router = . empty
16
14
17
15
var body : some View {
18
- NavigationView {
19
- VStack {
20
- Button ( " go1 " ) { route = . go1}
21
- Button ( " go2 " ) { route = . go2}
22
- Button ( " go3 " ) { route = . go3}
16
+ NavigationView {
17
+ VStack {
18
+ Button ( " go1 " ) { route = . go1 }
19
+ Button ( " go2 " ) { route = . go2 }
20
+ Button ( " go3 " ) { route = . go3 }
23
21
} . navigation ( route: $route)
24
22
} . navigationViewStyle ( . stack)
25
23
}
26
24
}
27
25
28
- extension View {
26
+ extension View {
29
27
@ViewBuilder
30
- func navigation( route : Binding < Router > ) -> some View {
31
- if route. wrappedValue != . empty{
28
+ func navigation( route: Binding < Router > ) -> some View {
29
+ if route. wrappedValue != . empty {
32
30
modifier ( NavigationModifire ( route: route) )
33
- } else { self }
31
+ } else { self }
34
32
}
35
33
}
36
34
37
35
// MARK: - define view modifire
38
- struct NavigationModifire : ViewModifier {
39
-
40
- @State var isActive : Bool = true
41
-
42
- @Binding var route : Router
43
-
36
+
37
+ struct NavigationModifire : ViewModifier {
38
+ @State var isActive : Bool = true
39
+
40
+ @Binding var route : Router
41
+
44
42
func body( content: Content ) -> some View {
45
43
content
46
- . background {
47
- NavigationLink ( destination : route. builder, isActive: $isActive ) {
44
+ . background {
45
+ NavigationLink ( destination: route. builder, isActive: $isActive) {
48
46
EmptyView ( )
49
47
} . hidden ( )
50
48
}
51
- . onChange ( of: isActive) {
49
+ . onChange ( of: isActive) {
52
50
if $0 == false { route = . empty }
53
51
}
54
52
}
55
-
56
53
}
57
54
58
55
// MARK: - Define routes
@@ -65,7 +62,7 @@ enum Router {
65
62
66
63
@ViewBuilder
67
64
var builder : some View {
68
- switch ( self ) {
65
+ switch self {
69
66
case . go1: SubView ( text: " go1 " )
70
67
case . go2: SubView ( text: " go2 " )
71
68
case . go3: SubView ( text: " go3 " )
@@ -77,16 +74,13 @@ enum Router {
77
74
// MARK: - Define sub view
78
75
79
76
struct SubView : View {
80
-
81
77
let text : String
82
78
83
79
var body : some View {
84
80
Text ( " \( text) " )
85
81
}
86
82
}
87
83
88
-
89
-
90
84
struct ContentView_Previews : PreviewProvider {
91
85
static var previews : some View {
92
86
ContentView ( )
0 commit comments