Как из функции при нажатии кнопки Ok вызвать открытие ContentView?
Есть такая функция:
func didFinishConfirmation(paymentMethodType: YooKassaPayments.PaymentMethodType) {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
let alertController = UIAlertController(
title: "Confirmation",
message: "успешно",
preferredStyle: .alert
)
let action = UIAlertAction(title: "OK", style: .default)
alertController.addAction(action)
self.dismiss(animated: true)
self.present(alertController, animated: true)
}
}
Как сделать так, чтобы при нажатии на кнопку Ok при появлении предупреждения открывался ContentView() в SwiftUI?
Ответы (1 шт):
Автор решения: Pavel Lazarev
→ Ссылка
У UIAlertController есть handler, то есть вам нужно реализовать так
let action = UIAlertAction(title: "OK", style: .default) { _ in
// здесь открытие экрана
}
для открытия экрана SwiftUI из UIKit нужно использовать UIHostingController:
let contentView = UIHostingController(rootView: ContentView())
код полностью:
func didFinishConfirmation(paymentMethodType: YooKassaPayments.PaymentMethodType) {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
let alertController = UIAlertController(
title: "Confirmation",
message: "успешно",
preferredStyle: .alert
)
let action = UIAlertAction(title: "OK", style: .default) { _ in
let contentView = UIHostingController(rootView: ContentView())
self.present(contentView, animated: true)
}
alertController.addAction(action)
self.dismiss(animated: true)
self.present(alertController, animated: true)
}
}