NEW: Learn to build the incredible iOS 15 Weather app today! >>

Ultimate Portfolio App: Tapping on Awards displays HomeView

Forums > Videos

Whenever I tap on any of the awards, the alert is shown but the app then displays the HomeView instead of showing the AwardsView. I have no idea why this is so. If I tap the button without an alert sheet, there are no issues so I assume the issue is related to the AlertSheet. Thoughts?

Not too sure if this is the correct place for it and I am unable to share a video of it either. But here is the code.

struct AwardsView: View {
    static let tag: String = "Awards"

    @EnvironmentObject var dataController: DataController
    @State private var selectedAward = Award.example
    @State private var showingAwardDetails = false

    var columns: [GridItem] {
        [GridItem(.adaptive(minimum: 100, maximum: 100))]
    }

    var body: some View {
        NavigationView {
            ScrollView {
                LazyVGrid(columns: columns) {
                    ForEach(Award.allAwards) { award in
                        Button {
                            selectedAward = award
                            showingAwardDetails = true
                        } label: {
                            Image(systemName: award.image)
                                .resizable()
                                .scaledToFit()
                                .padding()
                                .frame(width: 100, height: 100)
                                .foregroundColor(dataController.hasEarned(award: award) ? Color(award.color) : Color.secondary.opacity(0.5))
                        }
                    }
                }
            }
            .navigationTitle("Awards")
        }
        .alert(isPresented: $showingAwardDetails) {
            if dataController.hasEarned(award: selectedAward) {
                return Alert(title: Text("Unlocked: \(selectedAward.name)"), message: Text("\(Text(selectedAward.description))"), dismissButton: .default(Text("OK")))
            } else {
                return Alert(title: Text("Locked"), message: Text("\(Text(selectedAward.description))"), dismissButton: .default(Text("OK")))
            }
        }
    }
}

   

I haven't started the Ultimate Portfolio myself, but you are currently showing code that works as expected.

It is best to also provide the code that doesn't work as expected. The code we are going to help you debug 😉

   

That looks correct and this works when its in my project. (using Xcode v12.2)

Best bet is to Quit Xcode and retry. (maybe Xcode playing up)

   

Hacking with Swift is sponsored by Essential Developer

SPONSORED Only until this Sunday, December 12th, you can join a free crash course to learn advanced techniques for testing new and legacy Swift code — it's the fast track to becoming a complete Senior iOS Developer!

Click to learn more

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

Hi guys, sorry for not getting back sooner.

The code that I am sharing is the one that is not working i.e. its where the alert view is messing up the tab bar.

I've actually tried that a couple of times but to no avail. Sadly, Paul does not share his source code (or at least I don't think so..)

   

I understand this is the view where the alert is, and where it appears the problem is. However it doesn't mean that this is the code section that is giving you problems. The problem might be elsewhere. So when we look at this code, it seems to work. There is no clear reason for it not to.

It could be anything. I could be the simulator. Did you try on a real device? or on a different simulator?

   

Hmmm let me try a real device when I get back. The only thing I noticed is that removing the alert fixes the issue oddly.

   

Hmmm let me try a real device when I get back. The only thing I noticed is that removing the alert fixes the issue oddly.

Hence testing on a real device or sharing the entire code. The above code works when we tried it. So if it persists on a real device, then the only remaining explanation is to be found in the rest of the code.

When debugging I have found it best to start with an assumption, if it works out, great. If it doesn't or if I still don't understand why, check the rest of the code. It's a painstaking process but... well... welcome to coding 😉

   

Hi @MarcusKay. Thanks for sharing. I did try on a my device and it is till replicating the issue. I've tried browsing around my code and could not find a reason for it aside from removing the alert. Here is the repo https://github.com/StormKop/UltimatePortfolio. Appreciate any feedback you have :)

   

The bug was not strictly HomeView, but as soon as you tap on an award, it goes back to whichever tab was selected.

This helped in focusing in the issue. It had to do with the TabView, not just Home. That got me focused on the tags and when I compared to my code again, I noticed a tiny difference that fixed the issue.

struct AwardsView: View {
    static let tag: String? = "Awards"

We missed it... tag should be an Optional String...

1      

Oh my so blinded. Thanks @MarcusKay! I wonder why not having the optionality caused such an odd behaviour. It seems rather hard to catch.

   

It has to do with TabView. If you dig into the documentation you will find this:

init(selection: Binding<SelectionValue>?, content: () -> Content)

Which is why we declared our selectedView State property as optional. (I asked the same question after finding the bug 😉)

   

Hacking with Swift is sponsored by Essential Developer

SPONSORED Only until this Sunday, December 12th, you can join a free crash course to learn advanced techniques for testing new and legacy Swift code — it's the fast track to becoming a complete Senior iOS Developer!

Click to learn more

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.