Я хочу иметь класс A и класс B. Класс A (некоторый класс singleton manager) отвечает за создание экземпляра UIAlertController и передает его классу B (UIViewController) для его представления.Я нашел пример кода ниже, который использует расширение на UIAlertController для представления UIAlertController.Проблема в том, что в классе A (в настоящее время просто расширение) я не хочу ссылки на класс B (или любой viewController, используемый для представления alertController).Я хочу создать UIAlertController и вместо этого передать его в класс B.Возможно ли это?
extension UIAlertController { //<-- I want this to be Class A that instantiates UIAlertController, set it up using RX and pass it to Class B to present. Possible?
struct AlertAction {
var title: String?
var style: UIAlertActionStyle
static func action(title: String?, style: UIAlertActionStyle = .default) -> AlertAction {
return AlertAction(title: title, style: style)
}
}
static func present(
in viewController: UIViewController,//<---ViewController used to present the alertController
title: String?,
message: String?,
style: UIAlertControllerStyle,
actions: [AlertAction])
-> Observable<Int>
{
return Observable.create { observer in
let alertController = UIAlertController(title: title, message: message, preferredStyle: style)
actions.enumerated().forEach { index, action in
let action = UIAlertAction(title: action.title, style: action.style) { _ in
observer.onNext(index)
observer.onCompleted()
}
alertController.addAction(action)
}
viewController.present(alertController, animated: true, completion: nil)
return Disposables.create { alertController.dismiss(animated: true, completion: nil) }
}
}
}
Использование в классе B:
let disposeBag = DisposeBag()
let actions: [UIAlertController.AlertAction] = [
.action(title: "no"),
.action(title: "yes")
]
UIAlertController
.present(in: self, title: "Alert", message: "message", style: .alert, actions: actions)
.subscribe(onNext: { buttonIndex in
switch buttonIndex {
case 0:
//do stuff when I press no
case 1:
//do other stuff when I press yes
}
})
.disposed(by: disposeBag)