【问题标题】:SwiftUI: UIAlertController ActionSheet is full screenSwiftUI:UIAlertController ActionSheet 全屏
【发布时间】:2020-04-23 07:33:51
【问题描述】:

我正在尝试在 SwiftUI View 中实现一个选中标记的操作表。我正在使用一个 UIViewControllerRepresentable 创建一个UIAlertController

struct WhatsAppAlertController: UIViewControllerRepresentable {
    let viewModel: PropViewModel

    func makeUIViewController(context: Context) -> UIAlertController {
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        let contactsNumbers = viewModel.contactsNumbers()

        for number in contactsNumbers {
            let action = UIAlertAction(
                title: "\(number.value.stringValue)",
                style: .default,
                handler: { _ in
                self.viewModel.openWhatsAppURL(withNumber: number.value.stringValue)
            })
            alert.addAction(action)
        }

        let cancel = UIAlertAction(title: L10n.cancel, style: .cancel, handler: nil)

        alert.addAction(cancel)

        return alert 
    }

    func updateUIViewController(_ uiViewController: UIAlertController, context: Context) {
    }
}

它是使用显示的

.sheet(isPresented: $showWhatsAppActionSheet) {
            WhatsAppAlertController(viewModel: self.viewModel)
        }

我感觉这是因为 UIAlertController 是使用 .sheet 呈现的

我的计划是使用action.setValue(true, forKey: "checked") 勾选并记住所选选项。

有没有办法解决这个问题?或者也许只使用 SwiftUI 实现复选标记?

【问题讨论】:

  • This post 可能会有所帮助。
  • 谢谢!设法修复它。

标签: swift swiftui uialertcontroller swiftui-actionsheet


【解决方案1】:

我的错误是没有创建一个持有者控制器,一个UIViewController 来容纳UIAlertController。演示文稿也应该使用.background() 而不是.sheet()

这是更新后的代码:

struct WhatsappAlertController: UIViewControllerRepresentable {
    @Binding var show: Bool
    let viewModel: PropViewModel

    func makeUIViewController(context: UIViewControllerRepresentableContext<WhatsappAlertController>) -> UIViewController {
        return UIViewController() // holder controller - required to present alert
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<WhatsappAlertController>) {
        if self.show {

            let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
            let contactsNumbers = viewModel.contactsNumbers()

            for number in contactsNumbers {
                let action = UIAlertAction(
                    title: "\(number.value.stringValue)",
                    style: .default,
                    handler: { _ in
                        self.viewModel.openWhatsAppURL(withNumber: number.value.stringValue)
                })
//                action.setValue(true, forKey: "checked")
                alert.addAction(action)
            }

            let cancel = UIAlertAction(title: L10n.cancel, style: .cancel, handler: nil)

            alert.addAction(cancel)

            DispatchQueue.main.async { // must be async !!
                uiViewController.present(alert, animated: true, completion: {
                    self.show = false  // hide holder after alert dismiss
                })
            }
        }
    }
}

并显示:

.background(WhatsappAlertController(show: self.$showWhatsAppActionSheet, viewModel: viewModel))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2015-03-21
    • 2020-03-20
    • 2015-07-09
    • 1970-01-01
    相关资源
    最近更新 更多