Having completed the UIKit projects with their challenges, I decided to dip into SwiftUI. This is from WordScramble Challenges at wrap-up.
So adding a startGame()
button and a label showing the score was fine, as was ensuring the word was at least 3 letters and not the rootWord
. But the comment on calculating the score any way I wanted, got me thinking. What if I could add a button on the right of the navigation bar which explains how the score is calculated.
You have to use (leading: , trailing:)
in order to get it to work... a quick look:
.navigationBarItems(leading:
Button(action: startGame) {
HStack {
Image(systemName: "play.fill")
.foregroundColor(.blue)
Text("Start New")
}
},
trailing:
Button(action: showScoreDetailsAlert) {
Text("Score")
Image(systemName: "questionmark.circle")
})
Back to the explanation alert... it didn't deserve it's own view, so I thought, an alert would do just fine. Except it didn't.
At first I thought maybe it was the way I was handling the first challenge. I basically added a function that checks both conditions, and placed it in a guard
statement before all other checks.
Well, it turns out, you can not have 2 alerts on the same view. I basically just added my showScoreDetails
alert directly under the error alert. I tried attaching it to the TextField
but that didn't work either.
The solution was to place it after the closing brace of NavigationView
, and leaving the wrong words alert after the closing brace of the VStack.
Sharing it here, because it can be annoying digging for it elsewhere ;)