Popovers icon indicating copy to clipboard operation
Popovers copied to clipboard

Cannot auto alert when present init true

Open swanfly opened this issue 2 years ago • 1 comments

Use SwiftUI API can auto alert when page first loaded, but your code cannot.

SwiftUI API code: @State private var showAlert = true VStack(spacing: 0) { Text("首次启动弹出弹窗") } .alert("title", isPresented: $showAlert, actions: { Button("取消", role: .cancel, action: {}) }, message: { Text("Message") })

Your code: @State private var showAlert = true VStack(spacing: 0) { Text("首次启动弹出弹窗") } .popover(present: $showAlert) { Text("Message") }

swanfly avatar Jun 06 '22 02:06 swanfly

This is because Popovers listens to changes in present - you'll need to attach an onAppear and set showAlert there.

@State private var showAlert = false /// set to false first

.onAppear {
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        showAlert = true /// show it!
    }
}

aheze avatar Jun 07 '22 04:06 aheze