簡體   English   中英

SwiftUI:UIAlertController ActionSheet 為全屏

[英]SwiftUI: UIAlertController ActionSheet is full screen

我正在嘗試在 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 來實現復選標記?

在此處輸入圖像描述

我的錯誤是沒有創建一個持有人 controller,一個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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM