SwiftUI: UIAlertController ActionSheet - полный экран - PullRequest
0 голосов
/ 23 апреля 2020

Я пытаюсь реализовать проверенный помеченный лист действий в 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?

enter image description here

1 Ответ

0 голосов
/ 23 апреля 2020

Моя ошибка заключалась не в создании контроллера держателя, а 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))

...