Bird
Raised Fist0
iOS Swiftmobile~20 mins

First iOS app in iOS Swift - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Welcome Screen
This screen welcomes the user with a friendly message and a button that changes the message when tapped.
Target UI
---------------------
|                   |
|   Welcome to my   |
|      App!         |
|                   |
|   [Tap Me]        |
|                   |
---------------------
Show a centered label with the text 'Welcome to my App!'
Add a button below the label with the title 'Tap Me'
When the button is tapped, change the label text to 'You tapped the button!'
Use SwiftUI for the UI
Make sure the text and button are centered vertically and horizontally
Starter Code
iOS Swift
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()
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
iOS Swift
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.

Final Result
Completed Screen
---------------------
|                   |
|  You tapped the   |
|     button!       |
|                   |
|   [Tap Me]        |
|                   |
---------------------
When the user taps the 'Tap Me' button, the label text changes from 'Welcome to my App!' to 'You tapped the button!'
The button remains visible and can be tapped multiple times but the text stays changed
Stretch Goal
Add a reset button that appears after tapping 'Tap Me' to change the label back to the original text.
💡 Hint
Use another Button that sets the label text back to 'Welcome to my App!' and show it only after the first button is tapped.

Practice

(1/5)
1. What does the Text view do in a basic SwiftUI iOS app?
easy
A. It displays a string of text on the screen.
B. It creates a button that can be tapped.
C. It starts the app when launched.
D. It handles user input from the keyboard.

Solution

  1. Step 1: Understand the role of Text in SwiftUI

    The Text view is used to show words or sentences on the screen.
  2. Step 2: Differentiate from other views

    Unlike Button, which handles taps, Text only displays static text.
  3. Final Answer:

    It displays a string of text on the screen. -> Option A
  4. Quick Check:

    Text shows text [OK]
Hint: Remember: Text shows words, Button reacts to taps [OK]
Common Mistakes:
  • Confusing Text with Button
  • Thinking Text starts the app
  • Assuming Text handles user input
2. Which of the following is the correct way to create a button with the label "Tap me" in SwiftUI?
easy
A. Button("Tap me") { print("Tapped") }
B. Button(Text("Tap me")) { print("Tapped") }
C. Button { print("Tapped") } label: { Text("Tap me") }
D. Button(action: print("Tapped"), label: "Tap me")

Solution

  1. Step 1: Recall SwiftUI Button syntax

    The correct syntax uses a trailing closure for the action and a label closure for the button text.
  2. Step 2: Match syntax to options

    Button { print("Tapped") } label: { Text("Tap me") } matches the correct pattern: Button { action } label: { Text("label") }.
  3. Final Answer:

    Button { print("Tapped") } label: { Text("Tap me") } -> Option C
  4. Quick Check:

    Button syntax uses closures for action and label [OK]
Hint: Button uses closures: action first, label second [OK]
Common Mistakes:
  • Using Button with string only as label
  • Putting action inside label
  • Incorrect parameter order
3. What will be displayed when this SwiftUI code runs?
struct ContentView: View {
  @State private var count = 0
  var body: some View {
    VStack {
      Text("Count: \(count)")
      Button("Increment") {
        count += 1
      }
    }
  }
}
medium
A. A text showing "Count: 0" and a button labeled "Increment" that increases count when tapped.
B. Only a button labeled "Increment" with no text displayed.
C. A text showing "Count: 1" initially and a button labeled "Increment".
D. The app will crash due to missing @State annotation.

Solution

  1. Step 1: Understand @State usage

    The variable count is marked with @State, so changes update the UI.
  2. Step 2: Analyze initial UI

    Initially, count is 0, so text shows "Count: 0" and button labeled "Increment" is visible.
  3. Final Answer:

    A text showing "Count: 0" and a button labeled "Increment" that increases count when tapped. -> Option A
  4. Quick Check:

    @State variable updates UI on change [OK]
Hint: @State variables start at initial value and update UI [OK]
Common Mistakes:
  • Thinking count starts at 1
  • Assuming no text is shown
  • Believing app crashes without @State
4. Identify the error in this SwiftUI code snippet:
struct ContentView: View {
  var body: some View {
    Button("Click me") {
      print("Clicked")
    }
  }
}
medium
A. The body property must return some View explicitly.
B. No syntax error; the code is correct.
C. The body property must be marked with @ViewBuilder.
D. The Button label should be a closure, not a string.

Solution

  1. Step 1: Check body property syntax

    In SwiftUI, the body computed property implicitly returns the view using @ViewBuilder.
  2. Step 2: Verify the Button syntax

    Button("Click me") { print("Clicked") } is valid; no explicit return needed even for multi-line expressions.
  3. Final Answer:

    No syntax error; the code is correct. -> Option B
  4. Quick Check:

    SwiftUI body implicitly returns views [OK]
Hint: SwiftUI body uses implicit return via ViewBuilder [OK]
Common Mistakes:
  • Thinking an explicit return is required
  • Believing @ViewBuilder must be marked explicitly
  • Assuming Button requires closure label only
5. You want to create a simple iOS app that shows "Hello, SwiftUI!" and a button that changes the text to "Button tapped!" when pressed. Which code snippet correctly implements this behavior?
hard
A. struct ContentView: View { @State private var message = "Hello, SwiftUI!" var body: some View { VStack { Text("Hello, SwiftUI!") Button("Tap") { message = "Button tapped!" } } } }
B. struct ContentView: View { var message = "Hello, SwiftUI!" var body: some View { VStack { Text(message) Button("Tap") { message = "Button tapped!" } } } }
C. struct ContentView: View { var message = "Hello, SwiftUI!" var body: some View { VStack { Text(message) Button { message = "Button tapped!" } label: { Text("Tap") } } } }
D. struct ContentView: View { @State private var message = "Hello, SwiftUI!" var body: some View { VStack { Text(message) Button("Tap") { message = "Button tapped!" } } } }

Solution

  1. Step 1: Use @State for mutable text

    The text changes when the button is tapped, so message must be a @State variable.
  2. Step 2: Update text on button tap

    The button action updates message to "Button tapped!" and the UI reflects this change.
  3. 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, a VStack with Text(message), and a button with label and action correctly.
  4. Final Answer:

    A -> Option D
  5. Quick Check:

    @State variable updates text on button tap [OK]
Hint: Use @State for text that changes on button tap [OK]
Common Mistakes:
  • Not using @State for mutable text
  • Using constant text in Text view
  • Incorrect button label or action syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard