Skip to content

Commit 2ac14b7

Browse files
committed
nav
1 parent b4cf77f commit 2ac14b7

File tree

9 files changed

+103
-12
lines changed

9 files changed

+103
-12
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
*.xcuserstate
1+
*.xcuserstate
2+
*.xcworkspace
3+
*.pbxproj
4+
*.DS_Store

My App/My App.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
AB5C9B6D2ADBE02F00A358EC /* My_AppUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB5C9B6C2ADBE02F00A358EC /* My_AppUITests.swift */; };
1616
AB5C9B6F2ADBE02F00A358EC /* My_AppUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB5C9B6E2ADBE02F00A358EC /* My_AppUITestsLaunchTests.swift */; };
1717
AB5C9B7C2ADBE8AF00A358EC /* ButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB5C9B7B2ADBE8AF00A358EC /* ButtonView.swift */; };
18+
AB5C9B802ADBF38700A358EC /* ListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB5C9B7F2ADBF38700A358EC /* ListView.swift */; };
1819
/* End PBXBuildFile section */
1920

2021
/* Begin PBXContainerItemProxy section */
@@ -46,6 +47,7 @@
4647
AB5C9B6C2ADBE02F00A358EC /* My_AppUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = My_AppUITests.swift; sourceTree = "<group>"; };
4748
AB5C9B6E2ADBE02F00A358EC /* My_AppUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = My_AppUITestsLaunchTests.swift; sourceTree = "<group>"; };
4849
AB5C9B7B2ADBE8AF00A358EC /* ButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonView.swift; sourceTree = "<group>"; };
50+
AB5C9B7F2ADBF38700A358EC /* ListView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ListView.swift; path = "My App/ListView.swift"; sourceTree = SOURCE_ROOT; };
4951
/* End PBXFileReference section */
5052

5153
/* Begin PBXFrameworksBuildPhase section */
@@ -101,6 +103,7 @@
101103
AB5C9B552ADBE02F00A358EC /* Assets.xcassets */,
102104
AB5C9B572ADBE02F00A358EC /* Preview Content */,
103105
AB5C9B7B2ADBE8AF00A358EC /* ButtonView.swift */,
106+
AB5C9B7F2ADBF38700A358EC /* ListView.swift */,
104107
);
105108
path = "My App";
106109
sourceTree = "<group>";
@@ -260,6 +263,7 @@
260263
isa = PBXSourcesBuildPhase;
261264
buildActionMask = 2147483647;
262265
files = (
266+
AB5C9B802ADBF38700A358EC /* ListView.swift in Sources */,
263267
AB5C9B542ADBE02E00A358EC /* ContentView.swift in Sources */,
264268
AB5C9B522ADBE02E00A358EC /* My_AppApp.swift in Sources */,
265269
AB5C9B7C2ADBE8AF00A358EC /* ButtonView.swift in Sources */,

My App/My App/ButtonView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import SwiftUI
99

1010
struct ButtonView: View {
11+
var label: String
12+
1113
var body: some View {
1214
Button(action: {
1315
print("Button tapped")
1416
}, label: {
15-
Text("Tap me")
17+
Text(label)
1618
})
1719
}
1820
}

My App/My App/ContentView.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ import SwiftUI
99

1010
struct ContentView: View {
1111
var body: some View {
12-
VStack {
13-
Image(systemName: "globe")
14-
.imageScale(.large)
15-
.foregroundStyle(.tint)
16-
Text("Hello, world!")
17-
ButtonView()
12+
NavigationView {
13+
VStack {
14+
Image(systemName: "globe")
15+
.imageScale(.large)
16+
.foregroundStyle(.tint)
17+
Text("Hello, world!")
18+
ButtonView(label: "Tap me!")
19+
NavigationLink(destination: ListView(items: ["Item 1", "Item 2", "Item 3"])) {
20+
Text("Go to list")
21+
}
22+
}
23+
.padding()
1824
}
19-
.padding()
2025
}
2126
}
2227

My App/My App/ListView.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// ListView.swift
3+
// My App
4+
//
5+
// Created by Irfan Nafi on 10/15/23.
6+
//
7+
8+
import SwiftUI
9+
10+
struct ListView: View {
11+
var items: [String]
12+
13+
var body: some View {
14+
List(items, id: \.self) { item in
15+
Text(item)
16+
}
17+
}
18+
}

README.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,19 @@ struct ContentView: View {
9393

9494
1. Let's add a new view to the project that creates a button. Right click on the folder tree and click "New File...". Select "Swift File" and click "Next". Enter "ButtonView.swift" as the file name and click "Create". You should see a new file called `ButtonView.swift` in the folder tree.
9595

96-
2. In `ButtonView.swift`, add the following code:
96+
2. In `ButtonView.swift`, add the following shown below. By creating a `var label: String` property, we can pass in a label to the view. You can read more about properties [here](https://docs.swift.org/swift-book/LanguageGuide/Properties.html).
9797

9898
```swift
9999
import SwiftUI
100100

101101
struct ButtonView: View {
102+
var label: String
103+
102104
var body: some View {
103105
Button(action: {
104106
print("Button tapped")
105107
}, label: {
106-
Text("Tap me!")
108+
Text(label)
107109
})
108110
}
109111
}
@@ -112,13 +114,70 @@ struct ButtonView: View {
112114
3. Go back to `ContentView.swift`. Add the following code to the `VStack`, under `Text`:
113115

114116
```swift
115-
ButtonView()
117+
ButtonView(label: "Tap me!")
116118
```
117119

118120
4. Click the play button on the top left of the screen to run your app in the simulator. You should see a button that says "Tap me!". When you tap the button, you should see "Button tapped" printed in the console (which is at the bottom of the screen).
119121

120122
![](/hackpack-assets/console.png)
121123

124+
### Navigation
125+
126+
1. Navigation is a way to navigate between different views. Let's add a navigation view to the app. Go back to `ContentView.swift`. Wrap the `VStack` in a `NavigationView`. You can read more about it [here](https://developer.apple.com/documentation/swiftui/navigationview).
127+
128+
```swift
129+
NavigationView {
130+
VStack {
131+
Image(systemName: "globe")
132+
.imageScale(.large)
133+
.foregroundStyle(.tint)
134+
Text("Hello, world!")
135+
ButtonView(label: "Tap me!")
136+
}
137+
.padding()
138+
}
139+
```
140+
141+
2. Let's create a new screen that displays a list of items. Right click on the folder tree and click "New File...". Select "Swift File" and click "Next". Enter "ListView.swift" as the file name and click "Create". You should see a new file called `ListView.swift` in the folder tree.
142+
143+
3. In `ListView.swift`, add the following shown below. By creating a `var items: [String]` property, we can pass in a list of items to the view. You can read more about arrays [here](https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html#ID105).
144+
145+
```swift
146+
import SwiftUI
147+
148+
struct ListView: View {
149+
var items: [String]
150+
151+
var body: some View {
152+
List(items, id: \.self) { item in
153+
Text(item)
154+
}
155+
}
156+
}
157+
```
158+
159+
4. Go back to `ContentView.swift`. Add a navigation link to the `VStack`, under `ButtonView`. You can read more about it [here](https://developer.apple.com/documentation/swiftui/navigationlink).
160+
161+
```swift
162+
NavigationView {
163+
VStack {
164+
Image(systemName: "globe")
165+
.imageScale(.large)
166+
.foregroundStyle(.tint)
167+
Text("Hello, world!")
168+
ButtonView(label: "Tap me!")
169+
NavigationLink(destination: ListView(items: ["Item 1", "Item 2", "Item 3"])) {
170+
Text("Go to list")
171+
}
172+
}
173+
.padding()
174+
}
175+
```
176+
177+
5. Here's what the app should look like now:
178+
179+
![](/hackpack-assets/navigation.gif)
180+
122181
### Uploading to the App Store
123182

124183
# Congratulations! 🎉

hackpack-assets/navigation.gif

96.1 KB
Loading

0 commit comments

Comments
 (0)