UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Multiple Alerts - Lesson from personal challenge.

Forums > 100 Days of SwiftUI

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 ;)

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.