SwiftUI Catalyst警报显示重复的按钮且未触发动作

由于某些原因,以下代码显示的警报具有同一按钮的三个实例,没有一个实例会触发预期的操作:

SwiftUI Catalyst警报显示重复的按钮且未触发动作

还有其他人经历过吗?关于修复的任何建议吗?

它基于Xcode 11.2.1(针对iOS 13.0目标),然后通过Catalyst在macOS(10.15.1)上运行。

更新1:这似乎是Catalyst特有的问题。在iPhone模拟器上运行相同的代码时,它会显示一个按钮并按预期执行操作。

更新2:通过更新到Xcode 11.3和macOS 10.15.2也未解决该问题。

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
}
kissbabyfish 回答:SwiftUI Catalyst警报显示重复的按钮且未触发动作

您的代码似乎可以在 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

,

这似乎已在macOS Big Sur上修复。不幸的是,对于那些需要支持macOS Catalina(包括我)的人们,唯一的解决方法是使用UIAlertController创建警报。

我这样做的方式是向SceneDelegate实例分发通知,并在UIAlertController上显示UIHostingController

NotificationCenter.default.addObserver(forName: .showMailUnavailableAlert,object: nil,queue: nil) { [weak self] _ in
    let controller = UIAlertController(title: "Default email client is not configured.",preferredStyle: .alert)
    controller.addAction(.init(title: "Ok",style: .cancel,handler: nil))
    self?.window?.rootViewController?.present(controller,animated: true,completion: nil)
}

extension NSNotification.Name {
    static let showMailUnavailableAlert = NSNotification.Name("Email not configured.")
}
,

我不知道如何解决重复的按钮,但是要消除警报,您可能需要在ObservedObject行下添加以下行:

sudo add-apt-repository ppa:rafaeldtinoco/lp1871129
sudo apt update
sudo apt install libc6=2.31-0ubuntu8+lp1871129~1 -y
sudo apt-mark hold libc6

,然后添加以下内容:

@Environment(\.presentationMode) var presentationMode

到您的dismissSelf()函数。

这是我从Paul Hudson的Hacking Swift视频中收集的。

本文链接:https://www.f2er.com/2955500.html

大家都在问