SwiftUI State
This is the iOS-15-only version of the alert code representing the state of the project at the end of this episode:
.alert("Hello there!", isPresented: $alertIsVisible) {
Button("Awesome!") { }
} message: {
Text("This is my first pop-up")
}
If you continue building up the message
view until the end of the course, the result will look like this:
.alert("Hello there!", isPresented: $alertIsVisible) {
Button("Awesome!") { }
} message: {
let roundedValue = Int(sliderValue.rounded())
Text("The slider's value is \(roundedValue).\n" + "You scored \(game.points(sliderValue: roundedValue)) points this round.")
}
And finally, if you continue into the next course, Your First iOS and SwiftUI App: Polishing the App, this is what the final version of the code would look like, in Episode 27—right before you learn to create a custom view instead of relying on the alert
modifier.
.alert(
"Hello there!",
isPresented: $alertIsVisible,
presenting: {
let roundedValue = Int(sliderValue.rounded())
return (
roundedValue,
game.points(sliderValue: roundedValue)
)
} () as (roundedValue: Int, points: Int)
) { data in
Button("Awesome!") {
game.startNewRound(points: data.points)
}
} message: { data in
Text("The slider's value is \(data.roundedValue).\n" + "You scored \(data.points) points this round.")
}
Learn about an important concept called SwiftUI state, which helps you keep your user interface and app state always consistent.
Comments