Challenge - 5 Problems
TabView Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
TabView selection behavior
What will be the visible tab label when the user taps the second tab in this SwiftUI TabView?
iOS Swift
import SwiftUI struct ContentView: View { @State private var selection = 1 var body: some View { TabView(selection: $selection) { Text("Home Screen") .tabItem { Label("Home", systemImage: "house") } .tag(1) Text("Settings Screen") .tabItem { Label("Settings", systemImage: "gear") } .tag(2) } } }
Attempts:
2 left
💡 Hint
The selection state controls which tab is active and visible.
✗ Incorrect
The TabView uses the selection binding to track the active tab. When the user taps the second tab, the selection changes to 2, so the "Settings" tab label and content become visible.
📝 Syntax
intermediate2:00remaining
Correct syntax for TabView with three tabs
Which option shows the correct SwiftUI code to create a TabView with three tabs labeled "First", "Second", and "Third"?
Attempts:
2 left
💡 Hint
tabItem expects a view builder with a Label for accessibility and icon.
✗ Incorrect
Option A correctly uses the tabItem modifier with a Label view including text and systemImage. Other options have syntax errors or missing required parameters.
❓ lifecycle
advanced2:00remaining
State preservation in TabView
In a SwiftUI TabView with multiple tabs, what happens to the state of a tab's view when the user switches to another tab?
Attempts:
2 left
💡 Hint
Think about how SwiftUI keeps views alive inside TabView.
✗ Incorrect
SwiftUI keeps the views of all tabs alive in memory while the TabView exists, so their state is preserved when switching tabs.
advanced
2:00remaining
Programmatic tab selection
How can you programmatically switch to the third tab in a SwiftUI TabView?
iOS Swift
import SwiftUI struct ContentView: View { @State private var selection = 1 var body: some View { VStack { TabView(selection: $selection) { Text("First Tab").tabItem { Label("First", systemImage: "1.circle") }.tag(1) Text("Second Tab").tabItem { Label("Second", systemImage: "2.circle") }.tag(2) Text("Third Tab").tabItem { Label("Third", systemImage: "3.circle") }.tag(3) } Button("Go to Third Tab") { // What code goes here? } } } }
Attempts:
2 left
💡 Hint
Use the @State variable to update the selected tab.
✗ Incorrect
Inside the View struct, you must use self.selection = 3 to update the @State variable and switch tabs programmatically.
🔧 Debug
expert2:00remaining
Why does the TabView not switch tabs?
Given this SwiftUI code, why does tapping the tab items not change the visible tab?
iOS Swift
import SwiftUI struct ContentView: View { @State private var selection = 1 var body: some View { TabView(selection: .constant(selection)) { Text("First Tab").tabItem { Label("First", systemImage: "1.circle") }.tag(1) Text("Second Tab").tabItem { Label("Second", systemImage: "2.circle") }.tag(2) } } }
Attempts:
2 left
💡 Hint
Check how the selection binding is passed to TabView.
✗ Incorrect
Using .constant(selection) creates a read-only binding, so TabView cannot update the selection state when tabs are tapped, preventing tab switching.