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

SOLVED: Conditional Alerts

Forums > SwiftUI

Hey All,

The knowledge from the community has been incredible in helping me learn and develop. To everyone who has provided feedback so far, sincerely, thank you.

I'm working on an issue that I've spent about half the day trying to solve and can't figure it out. In short, I'd like to present alerts that are conditional to an if statement. The rub is, that while I can validate that .onTapGesture is working in either state, I'm not getting the second alert to show if the condition falls to the else statement, despite getting a console print that validates the object was tapped.

I searched through HWS and the forums but didn't find anything on this matter specifically. So I defaulted to Stack and this seemed to be what a lot of archived projects had recommended. I know each view can only have one alert, but since the view presented is conditional I'm assuming that this would hold true in the manner I've done this.... right? Can anyone provide some insight into what is happening?

Thanks!

VStack {
                Text("Night Time")
                    .font(.subheadline)
                    .frame(alignment: .center)
                if Hours().doubleNightHours > 3.0 {
                    Image(systemName: "checkmark.circle")
                        .font(.system(size: 25))
                        .foregroundColor(.green)
                        .frame(width: 100, alignment: .center)
                        .onTapGesture {
                            nightReqMet = true
                            print("Tapped")
                        }
                } else {
                    Image(systemName: "x.circle.fill")
                        .font(.system(size: 25))
                        .foregroundColor(.red)
                        .frame(width: 100, alignment: .center)
                        .onTapGesture {
                            nightExplanationShowing = true
                            print("Tapped")
                        }
                }
            }
            .frame(width: 100, alignment: .center)
            .alert(isPresented: $nightExplanationShowing, content: {
                Alert(title: Text("14 CFR Part 61.109(a)(2)"), message: Text("Subsection 109 requires that private pilot applicants have a minimum of 3 hours of night training experience including a 100nm cross-country flight, and 10 total takeoffs and landings at an airport."), dismissButton: .default(Text("Dismiss")))
            })
            .alert(isPresented: $nightReqMet, content: {
                Alert(title: Text("You've Met This Requirement!"), message: Text("Congratulations! You've met the requirements for night experience per 14 CFR Part 61.109(a)(2)!"), dismissButton: .default(Text("Dismiss")))
            })

2      

Hi,

I dont know why it happens but attaching them to their respective image works.

VStack {
    Text("Night Time")
        .font(.subheadline)
        .frame(alignment: .center)
    if Hours().doubleNightHours > 3.0 {
        Image(systemName: "checkmark.circle")
            .font(.system(size: 25))
            .foregroundColor(.green)
            .frame(width: 100, alignment: .center)
            .onTapGesture {
                nightReqMet = true
                print("Tapped")
            }
            .alert(isPresented: $nightReqMet, content: {
                Alert(title: Text("You've Met This Requirement!"),
                      message: Text("Congratulations! You've met the requirements for night experience per 14 CFR Part 61.109(a)(2)!"),
                      dismissButton: .default(Text("Dismiss")))
            })
    } else {
        Image(systemName: "x.circle.fill")
            .font(.system(size: 25))
            .foregroundColor(.red)
            .frame(width: 100, alignment: .center)
            .onTapGesture {
                nightExplanationShowing = true
                print("Tapped")
            }
            .alert(isPresented: $nightExplanationShowing, content: {
                Alert(title: Text("14 CFR Part 61.109(a)(2)"),
                      message: Text("Subsection 109 requires that private pilot applicants have a minimum of 3 hours of night training experience including a 100nm cross-country flight, and 10 total takeoffs and landings at an airport."),
                      dismissButton: .default(Text("Dismiss")))
            })
    }
}

2      

Thanks for the help! I think this did the trick. Can't figure out why it had to be written this way specifically but it seems to be working now!

Andrew

2      

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!

Reply to this topic…

You need to create an account or log in to reply.

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.