HomeiOS Developmentios - My SwiftUI app doesn't dynamically populate the content material of...

ios – My SwiftUI app doesn’t dynamically populate the content material of a pop-up View when the primary button is pressed


I’ve a grid of small pictures/icons as buttons and a pop-up modal View. The modal view is supposed to show a bigger model of the picture that was the button, and a few details about it. Nonetheless, in observe, the primary button pressed simply brings up an empty View. Solely after closing it and urgent a unique button is the view populated. From that time ahead, the app works as anticipated. I assume it is due to the asynchronous nature of the sheet modifier, however I can not seem to get a deal with on how one can populate the modal View earlier than it’s displayed the primary time.

Here’s a drastically simplified model of the code (with two easy textual content buttons) that displays the identical habits, so you possibly can see what I am doing.

import SwiftUI

struct ContentView: View {
    @State personal var showModal = false
    @State personal var selectedItem = ""
    
    var physique: some View {
        VStack {
            Button(motion: {
                self.selectedItem = "Button 1"
                self.showModal = true
            }) {
                Textual content("Button 1")
            }
            Button(motion: {
                self.selectedItem = "Button 2"
                self.showModal = true
            }) {
                Textual content("Button 2")
            }
        }
        .sheet(isPresented: $showModal, content material: {
            ModalView(merchandise: self.selectedItem)
        })
    }
}

struct ModalView: View {
    let merchandise: String
    
    var physique: some View {
        VStack {
            Textual content("You pushed (merchandise)")
        }
    }
}

On this circumstance, if I faucet “Button 1”, it simply opens a view that simply says “You pushed “. If I shut that modal and faucet it once more, the identical factor occurs. Solely once I faucet “Button 2” does the modal view pop up and say “You pushed Button 2” and after that time, it additionally says “You pushed Button 1” once I faucet “Button 1”.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments