我的SwiftUI应用在第一次按下按钮时没有动态填充弹出视图的内容。

huangapple go评论69阅读模式
英文:

My SwiftUI app does not dynamically populate the content of a pop-up View when the first button is pressed

问题

I have a grid of small images/icons as buttons and a pop-up modal View. The modal view is meant to display a larger version of the image that was the button, and some information about it. However, in practice, the first button pressed just brings up an empty View. Only after closing it and pressing a different button is the view populated. From that point forward, the app works as expected. I assume it's because of the asynchronous nature of the sheet modifier, but I can't seem to get a handle on how to populate the modal View before it is displayed the first time.

Here is a drastically simplified version of the code (with two simple text buttons) that exhibits the same behavior, so you can see what I'm doing.

import SwiftUI

struct ContentView: View {
    @State private var showModal = false
    @State private var selectedItem = ""

    var body: some View {
        VStack {
            Button(action: {
                self.selectedItem = "Button 1"
                self.showModal = true
            }) {
                Text("Button 1")
            }
            Button(action: {
                self.selectedItem = "Button 2"
                self.showModal = true
            }) {
                Text("Button 2")
            }
        }
        .sheet(isPresented: $showModal, content: {
            ModalView(item: self.selectedItem)
        })
    }
}

struct ModalView: View {
    let item: String

    var body: some View {
        VStack {
            Text("You pushed \(item)")
        }
    }
}

In this circumstance, if I tap "Button 1", it just opens a view that just says "You pushed ". If I close that modal and tap it again, the same thing happens. Only when I tap "Button 2" does the modal view pop up and say "You pushed Button 2" and after that point, it also says "You pushed Button 1" when I tap "Button 1".

英文:

I have a grid of small images/icons as buttons and a pop-up modal View. The modal view is meant to display a larger version of the image that was the button, and some information about it. However, in practice, the first button pressed just brings up an empty View. Only after closing it and pressing a different button is the view populated. From that point forward, the app works as expected. I assume it's because of the asynchronous nature of the sheet modifier, but I can't seem to get a handle on how to populate the modal View before it is displayed the first time.

Here is a drastically simplified version of the code (with two simple text buttons) that exhibits the same behavior, so you can see what I'm doing.

import SwiftUI

struct ContentView: View {
    @State private var showModal = false
    @State private var selectedItem = ""
    
    var body: some View {
        VStack {
            Button(action: {
                self.selectedItem = "Button 1"
                self.showModal = true
            }) {
                Text("Button 1")
            }
            Button(action: {
                self.selectedItem = "Button 2"
                self.showModal = true
            }) {
                Text("Button 2")
            }
        }
        .sheet(isPresented: $showModal, content: {
            ModalView(item: self.selectedItem)
        })
    }
}

struct ModalView: View {
    let item: String
    
    var body: some View {
        VStack {
            Text("You pushed \(item)")
        }
    }
}

In this circumstance, if I tap "Button 1", it just opens a view that just says "You pushed ". If I close that modal and tap it again, the same thing happens. Only when I tap "Button 2" does the modal view pop up and say "You pushed Button 2" and after that point, it also says "You pushed Button 1" when I tap "Button 1".

答案1

得分: 1

尝试使用:

@State private var selectedItem: String? 

 

.sheet(item: $selectedItem){ item in 
     ModalView(item: item)
} 

代替。不需要使用 `showModal`
英文:

try using

@State private var selectedItem: String? 

and

.sheet(item: $selectedItem){ item in 
     ModalView(item: item)
 } 

instead. No need for showModal

huangapple
  • 本文由 发表于 2023年5月21日 14:48:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定