gpt4 book ai didi

ios - SwiftUI:显示重复按钮且未触发操作的催化剂警报

转载 作者:行者123 更新时间:2023-12-01 15:28:41 24 4
gpt4 key购买 nike

出于某种原因,以下代码显示了具有相同按钮的三个实例的警报,但没有一个按预期触发操作(例如,只是一个简单的控制台输出):
Duplicate Buttons in Alert
有没有其他人经历过这个?关于修复的任何建议?
它基于 Xcode 11.2.1 构建,用于 iOS 13.0 目标,然后通过 Catalyst 在 macOS (10.15.1) 上运行。
更新 1:这似乎是 Catalyst 特有的问题。当相同的代码在 iPhone 模拟器上运行时,它会显示一个按钮并按预期执行操作。
更新 2:更新到 Xcode 11.3.1 和 macOS 10.15.3 也没有解决这个问题。

public struct ContactUsView: View {

@ObservedObject private var contactUsVM: ContactUsViewModel

private var successAlert: Alert {
Alert(
title: Text("Email Sent"),
message: Text("Thanks for taking the time to reach out to us. We appreciate it!"),
dismissButton: .default(Text("OK")) {
self.dismissSelf()
}
)
}

public var body: some View {
Form {
// ...
}
.alert(isPresented: self.$contactUsVM.contactAttemptSucceeded) {
self.successAlert
}
}

public init() {
self.contactUsVM = ContactUsViewModel()
}

private func dismissSelf() {
print("Dismissing!")
}
}

class ContactUsViewModel: ObservableObject {

@Published var contactAttemptSucceeded: Bool = true
}

最佳答案

看来您的代码在 上运行良好xCode 11.5 MacOs 0.15.4 .如果您运行您的示例(我刚刚填补了代码中的漏洞):

import SwiftUI

public struct ContactUsView: View {

@ObservedObject private var contactUsVM: ContactUsViewModel

private var successAlert: Alert {
Alert(
title: Text("Email Sent"),
message: Text("Thanks for taking the time to reach out to us. We appreciate it!"),
dismissButton: .default(Text("OK")) {
self.dismissSelf()
}
)
}

public var body: some View {
Form {
Text("Hello World")
}
.alert(isPresented: self.$contactUsVM.contactAttemptSucceeded) {
self.successAlert
}
}

public init() {
self.contactUsVM = ContactUsViewModel()
}

private func dismissSelf() {
print("Dismissing!")
}
}

class ContactUsViewModel: ObservableObject {

@Published var contactAttemptSucceeded: Bool = true
}

你会看到这个:

enter image description here

关于ios - SwiftUI:显示重复按钮且未触发操作的催化剂警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com