--------------------- | | | Welcome to my | | App! | | | | [Tap Me] | | | ---------------------
First iOS app in iOS Swift - Mini App: Build & Ship
import SwiftUI struct ContentView: View { // TODO: Add state variable for label text var body: some View { VStack { // TODO: Add Text view for label // TODO: Add Button view } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.white) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
import SwiftUI struct ContentView: View { @State private var labelText = "Welcome to my App!" var body: some View { VStack(spacing: 20) { Spacer() Text(labelText) .font(.title) .multilineTextAlignment(.center) Button("Tap Me") { labelText = "You tapped the button!" } .padding() .background(Color.blue) .foregroundColor(.white) .cornerRadius(8) Spacer() } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.white) .ignoresSafeArea() } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
We use a @State variable labelText to hold the text shown on the screen. This allows the text to update when the button is tapped.
The VStack arranges the label and button vertically with some spacing. The Text view shows the current label text, and the Button changes the text when tapped.
We style the button with padding, background color, white text, and rounded corners for a friendly look.
The whole stack is centered vertically and horizontally using Spacer() views inside the VStack along with frame(maxWidth: .infinity, maxHeight: .infinity) to ensure the content is in the middle of the screen.
This simple app welcomes the user and responds to their tap by changing the message.
--------------------- | | | You tapped the | | button! | | | | [Tap Me] | | | ---------------------
Practice
Text view do in a basic SwiftUI iOS app?Solution
Step 1: Understand the role of
TheTextin SwiftUITextview is used to show words or sentences on the screen.Step 2: Differentiate from other views
UnlikeButton, which handles taps,Textonly displays static text.Final Answer:
It displays a string of text on the screen. -> Option AQuick Check:
Textshows text [OK]
- Confusing Text with Button
- Thinking Text starts the app
- Assuming Text handles user input
Solution
Step 1: Recall SwiftUI Button syntax
The correct syntax uses a trailing closure for the action and a label closure for the button text.Step 2: Match syntax to options
Button { print("Tapped") } label: { Text("Tap me") } matches the correct pattern:Button { action } label: { Text("label") }.Final Answer:
Button { print("Tapped") } label: { Text("Tap me") } -> Option CQuick Check:
Button syntax uses closures for action and label [OK]
- Using Button with string only as label
- Putting action inside label
- Incorrect parameter order
struct ContentView: View {
@State private var count = 0
var body: some View {
VStack {
Text("Count: \(count)")
Button("Increment") {
count += 1
}
}
}
}Solution
Step 1: Understand @State usage
The variablecountis marked with@State, so changes update the UI.Step 2: Analyze initial UI
Initially,countis 0, so text shows "Count: 0" and button labeled "Increment" is visible.Final Answer:
A text showing "Count: 0" and a button labeled "Increment" that increases count when tapped. -> Option AQuick Check:
@State variable updates UI on change [OK]
- Thinking count starts at 1
- Assuming no text is shown
- Believing app crashes without @State
struct ContentView: View {
var body: some View {
Button("Click me") {
print("Clicked")
}
}
}Solution
Step 1: Check body property syntax
In SwiftUI, thebodycomputed property implicitly returns the view using@ViewBuilder.Step 2: Verify the Button syntax
Button("Click me") { print("Clicked") }is valid; no explicitreturnneeded even for multi-line expressions.Final Answer:
No syntax error; the code is correct. -> Option BQuick Check:
SwiftUI body implicitly returns views [OK]
body uses implicit return via ViewBuilder [OK]- Thinking an explicit
returnis required - Believing
@ViewBuildermust be marked explicitly - Assuming Button requires closure label only
Solution
Step 1: Use @State for mutable text
The text changes when the button is tapped, somessagemust be a@Statevariable.Step 2: Update text on button tap
The button action updatesmessageto "Button tapped!" and the UI reflects this change.Step 3: Verify correct syntax
struct ContentView: View { @State private var message = "Hello, SwiftUI!" var body: some View { VStack { Text(message) Button("Tap") { message = "Button tapped!" } } } } uses@State private var, aVStackwithText(message), and a button with label and action correctly.Final Answer:
A -> Option DQuick Check:
@State variable updates text on button tap [OK]
- Not using @State for mutable text
- Using constant text in Text view
- Incorrect button label or action syntax
Ready to prove it?
Pass the test and this pattern moves unknown
