【问题标题】:SwiftUI: Catalyst Alert Showing Duplicate Buttons and Not Triggering ActionSwiftUI:显示重复按钮且未触发操作的催化剂警报
【发布时间】:2020-04-01 13:00:42
【问题描述】:

出于某种原因,以下代码显示了一个带有 三个 相同按钮实例的警报,但没有一个按预期触发操作(例如,只是一个简单的控制台输出):

有其他人经历过吗?有什么修复建议吗?

它基于 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.4.1 和 MacOS 10.15.3 对我来说类似的问题,除了我的 Alert 有三组主要和次要按钮。这只是在 Mac 上,在 iOS 上看起来不错。
  • 我也有同样的问题。

标签: ios macos alert swiftui mac-catalyst


【解决方案1】:

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

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

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.")
}

【讨论】:

    【解决方案2】:

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

    你会看到这个:

    【讨论】:

      【解决方案3】:

      我不知道如何修复重复按钮,但要让警报消失,您可能需要在 ObservedObject 行下添加此行:

      @Environment(\.presentationMode) var presentationMode
      

      然后添加:

      presentationMode.wrappedValue.dismiss()
      

      到您的 dismissSelf() 函数。

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

      【讨论】:

      • 谢谢,但那部分我知道。关键是该方法甚至没有被调用来进行日志输出。功能本身只是损坏警报的次要考虑因素。
      猜你喜欢
      • 2021-04-02
      • 2021-05-07
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多