Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to present a sheet when the button is tapped.
iOS Swift
Button("Show Sheet") { showSheet = true } .sheet(isPresented: $[1]) { Text("Hello from Sheet") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable that is not a binding boolean.
Forgetting the $ sign before the variable name.
✗ Incorrect
The .sheet modifier uses a binding boolean to control its presentation. Here, 'showSheet' is the correct state variable.
2fill in blank
mediumComplete the code to present a fullScreenCover when the button is tapped.
iOS Swift
Button("Show Full Screen") { [1] = true } .fullScreenCover(isPresented: $showFullScreen) { Text("Hello Full Screen") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name that does not match the binding.
Forgetting to set the variable to true on button tap.
✗ Incorrect
The button sets the 'showFullScreen' boolean to true to trigger the fullScreenCover presentation.
3fill in blank
hardFix the error in the code to correctly dismiss the sheet.
iOS Swift
struct ContentView: View {
@State private var showSheet = false
var body: some View {
Button("Show Sheet") {
showSheet = true
}
.sheet(isPresented: $showSheet) {
Button("Dismiss") {
[1] = false
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to dismiss by setting a different variable.
Using 'self.showSheet' inside the closure without explicit self capture.
✗ Incorrect
To dismiss the sheet, set the same state variable controlling its presentation to false.
4fill in blank
hardFill both blanks to create a sheet that passes data and dismisses itself.
iOS Swift
struct DetailView: View {
@Environment(\.[1]) var dismiss
var message: String
var body: some View {
VStack {
Text(message)
Button("Close") {
[2]()
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using presentationMode instead of dismiss in iOS 15+.
Not calling dismiss as a function.
✗ Incorrect
Use @Environment(\.dismiss) to get the dismiss action, then call dismiss() to close the sheet.
5fill in blank
hardFill all three blanks to present a fullScreenCover with a dismiss button and pass a title.
iOS Swift
struct ContentView: View {
@State private var showFullScreen = false
var body: some View {
Button("Open Full Screen") {
[1] = true
}
.fullScreenCover(isPresented: $showFullScreen) {
FullScreenView(title: [2], onDismiss: [3])
}
}
}
struct FullScreenView: View {
var title: String
var onDismiss: () -> Void
var body: some View {
VStack {
Text(title)
Button("Close") {
onDismiss()
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong variable names.
Not passing a closure for onDismiss.
Using showSheet instead of showFullScreen.
✗ Incorrect
Set 'showFullScreen' to true to present. Pass a string title and a closure to set 'showFullScreen' to false to dismiss.