Skip to content

Commit c1e2c65

Browse files
committed
button
1 parent ae696cb commit c1e2c65

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

My App/My App.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
AB5C9B632ADBE02F00A358EC /* My_AppTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB5C9B622ADBE02F00A358EC /* My_AppTests.swift */; };
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 */; };
17+
AB5C9B7C2ADBE8AF00A358EC /* ButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB5C9B7B2ADBE8AF00A358EC /* ButtonView.swift */; };
1718
/* End PBXBuildFile section */
1819

1920
/* Begin PBXContainerItemProxy section */
@@ -44,6 +45,7 @@
4445
AB5C9B682ADBE02F00A358EC /* My AppUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "My AppUITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
4546
AB5C9B6C2ADBE02F00A358EC /* My_AppUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = My_AppUITests.swift; sourceTree = "<group>"; };
4647
AB5C9B6E2ADBE02F00A358EC /* My_AppUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = My_AppUITestsLaunchTests.swift; sourceTree = "<group>"; };
48+
AB5C9B7B2ADBE8AF00A358EC /* ButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonView.swift; sourceTree = "<group>"; };
4749
/* End PBXFileReference section */
4850

4951
/* Begin PBXFrameworksBuildPhase section */
@@ -98,6 +100,7 @@
98100
AB5C9B532ADBE02E00A358EC /* ContentView.swift */,
99101
AB5C9B552ADBE02F00A358EC /* Assets.xcassets */,
100102
AB5C9B572ADBE02F00A358EC /* Preview Content */,
103+
AB5C9B7B2ADBE8AF00A358EC /* ButtonView.swift */,
101104
);
102105
path = "My App";
103106
sourceTree = "<group>";
@@ -259,6 +262,7 @@
259262
files = (
260263
AB5C9B542ADBE02E00A358EC /* ContentView.swift in Sources */,
261264
AB5C9B522ADBE02E00A358EC /* My_AppApp.swift in Sources */,
265+
AB5C9B7C2ADBE8AF00A358EC /* ButtonView.swift in Sources */,
262266
);
263267
runOnlyForDeploymentPostprocessing = 0;
264268
};

My App/My App/ButtonView.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// ButtonView.swift
3+
// My App
4+
//
5+
// Created by Irfan Nafi on 10/15/23.
6+
//
7+
8+
import SwiftUI
9+
10+
struct ButtonView: View {
11+
var body: some View {
12+
Button(action: {
13+
print("Button tapped")
14+
}, label: {
15+
Text("Tap me")
16+
})
17+
}
18+
}

My App/My App/ContentView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct ContentView: View {
1414
.imageScale(.large)
1515
.foregroundStyle(.tint)
1616
Text("Hello, world!")
17+
ButtonView()
1718
}
1819
.padding()
1920
}

README.md

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ In this hackpack we will cover the basics of SwiftUI and create a simple iOS app
2828

2929
### Overview of Xcode
3030

31-
1. On the left side of the screen, you should see a file navigator. This is where you can view all of the files in your project and also create new files/folders. Above the folder tree, you should see a row of buttons that show you different properties of the project. For example, you can see all the breakpoints or monitor the memory usage of your app.
31+
1. On the left side of the screen, you should see a file navigator. This is where you can view all of the files in your project and also create new files/folders. Above the folder tree, you should see a row of buttons that show you different properties of the project. For example, you can see all the breakpoints or monitor the memory usage of your app.
3232

3333
![](/hackpack-assets/folder_tree.png) ![](/hackpack-assets/performance.png)
3434

35-
2. At the very top, you should see the current device Xcode would run your app on. You can click this to change the device.
35+
2. At the very top, you should see the current device Xcode would run your app on. You can click this to change the device.
3636

3737
![](/hackpack-assets/device.png)
3838

@@ -46,6 +46,78 @@ In this hackpack we will cover the basics of SwiftUI and create a simple iOS app
4646

4747
3. Let's go back to `ContentView.swift`. You'll notice that it's a struct that conforms to the `View` protocol. This means that it's a view. You can see the code for the view on the right side of the screen. The `body` property is a computed property that returns a `some View`. This means that the type of the view is unknown, but it conforms to the `View` protocol. The `some` keyword is used to hide the type of the view. You can read more about opaque return types [here](https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html).
4848

49+
```swift
50+
import SwiftUI
51+
52+
struct ContentView: View {
53+
var body: some View {
54+
VStack {
55+
Image(systemName: "globe")
56+
.imageScale(.large)
57+
.foregroundStyle(.tint)
58+
Text("Hello, world!")
59+
}
60+
.padding()
61+
}
62+
}
63+
64+
#Preview {
65+
ContentView()
66+
}
67+
```
68+
69+
4. Within the `body` property, you'll see a `VStack`. This is a view that stacks its children vertically. You can read more about it [here](https://developer.apple.com/documentation/swiftui/vstack). Within the `VStack`, you'll see an `Image` and a `Text`. These are both views. You can read more about `Image` [here](https://developer.apple.com/documentation/swiftui/image) and `Text` [here](https://developer.apple.com/documentation/swiftui/text). You'll also see a modifier called `padding`. This is a modifier that adds padding to the view. You can read more about it [here](<https://developer.apple.com/documentation/swiftui/view/padding(_:edges:)>).
70+
71+
5. A modifier is a method that returns a modified version of the view. You can chain multiple modifiers together to modify the view. For example, you can add a background color to the view by adding a modifier called `background` to the end of the view. You can read more about it [here](<https://developer.apple.com/documentation/swiftui/view/background(_:alignment:)>). The order of the modifiers matters. If you add the `background` modifier before the `padding` modifier, the background color will only be applied to the view itself, not the padding. If you add the `background` modifier after the `padding` modifier, the background color will be applied to the view and the padding.
72+
73+
```swift
74+
import SwiftUI
75+
76+
struct ContentView: View {
77+
var body: some View {
78+
VStack {
79+
Image(systemName: "globe")
80+
.imageScale(.large)
81+
.foregroundStyle(.tint)
82+
Text("Hello, world!")
83+
}
84+
.padding()
85+
.background(Color.red)
86+
}
87+
}
88+
```
89+
90+
6. At the very bottom of the file, there's a `#Preview` struct (albeit with special syntax) that conforms to the `PreviewProvider` protocol. This struct is what creates the preview on the right side of the screen.
91+
92+
### Creating a New View
93+
94+
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.
95+
96+
2. In `ButtonView.swift`, add the following code:
97+
98+
```swift
99+
import SwiftUI
100+
101+
struct ButtonView: View {
102+
var body: some View {
103+
Button(action: {
104+
print("Button tapped")
105+
}, label: {
106+
Text("Tap me!")
107+
})
108+
}
109+
}
110+
```
111+
112+
3. Go back to `ContentView.swift`. Add the following code to the `VStack`, under `Text`:
113+
114+
```swift
115+
ButtonView()
116+
```
117+
118+
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).
119+
120+
![](/hackpack-assets/console.png)
49121

50122
### Uploading to the App Store
51123

hackpack-assets/console.png

103 KB
Loading

0 commit comments

Comments
 (0)