Your next career begins with
Save 50% off your seat in our next iOS Bootcamp. Limited time only. Sessions start April 3.
Your First iOS & SwiftUI App: An App from Scratch
Jan 11 2022 Swift 5.5, iOS 15, Xcode 13
Part 2: SwiftUI Data
13. Challenge: SwiftUI State

Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
About this episode
Practice what you’ve learned so far by adding a knock-knock joke to your app.
Instructors
Contributors
Instructor
Instructor
Illustrator
Video Editor
Over 300 content creators. Join our team.
Version history
iOS 15, Swift 5.5, Xcode 13 (Selected)
Jan 11 2022iOS 16, Swift 5.7, Xcode 14
Feb 13 2023iOS 14, Swift 5, Xcode 12
Dec 15 2020iOS 13, Swift 5, Xcode 11
Sep 3 2019iOS 12, Swift 4, Xcode 10
Jul 24 2018iOS 11, Swift 4, Xcode 9
Sep 19 2017iOS 10, Swift 3, Xcode 8
Nov 7 2016Swift 2
Sep 1 2015Leave a rating/review
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
This video Challenge: SwiftUI State was last updated on Jan 11 2022
Guess what - it’s time for your next coding challenge!
Your challenge is to pause the video, and add another button to the screen that says “Knock, Knock”. If you tap the button, the app should pop up a message that says “Who’s There?” in its title, and a knock knock joke of your choice in the message and dismiss button. I hope you have your best jokes ready!
Let’s review what you need to do for this:
First, open the SwiftUI canvas, open the library, and drag a button into the app, below the “Hit Me” button but still within the main VStack. Change its content to a text view that says “Knock Knock”.
Then, write some code to display a popup that says “Who’s There?”. Luckily, you already have an example of exactly how to do this:
You already have an example of how to add a state property to your ContentView struct. You can copy and paste this, and rename it to whosThereIsVisible.
You also already have an example of how to run some code when a button is tapped - by putting it inside the action block. This is where you wan to set whosThereIsVisible to true.
Finally, you already have an example of how to present an alert. Just follow this example to use whosThereIsVisible and present a knock knock joke instead.
Allright that’s it - now pause the video, and good luck.
Add Knock Knock button with SwiftUI canvas, set text to:
Text("Knock Knock")
Add whos there property:
@State private var whosThereIsVisible: Bool = false
Set it to true in the button’s action:
self.whosThereIsVisible = true
Add alert as follows:
.alert(isPresented: $whosThereIsVisible) {
return Alert(
title: Text("Who's There?"),
message: Text("Little old lady."),
dismissButton: .default(Text("Little old lady who?")))
}
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development — plans start at just $19.99/month! Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.
Learn more