gpt4 book ai didi

ios - 如何获得 UIAlertController observable(ReactiveCocoa 或 RxSwift)?

转载 作者:可可西里 更新时间:2023-11-01 03:27:37 25 4
gpt4 key购买 nike

我实现了一个“响应式(Reactive)”UIAlertController这样我就能得到 Observable<Int>按下按钮。 (见下面的代码)。

我的问题是:

  • 这个实现是否正确?我不喜欢存储观察者;我想知道是否有更好的解决方案。
  • 或者...是否已经在 ReactiveCocoa 或 RxSwift 中实现了这个?

这里是实现。我删除了与问题无关的部分。

class AlertBuilder {

typealias AlertAction = (Int) -> ()

private let alert: UIAlertController

/** If observable() is called, we keep here the observers to notify them */
private var observers: [AnyObserver<Int>] = []

init(alert: UIAlertController) {
self.alert = alert
}

/** When using observable(), the action is not needed. */
func button(_ title: String, style: UIAlertActionStyle = .default, action: AlertAction? = nil) -> AlertBuilder {

let buttonIndex = alert.actions.count
alert.addAction( UIAlertAction(title: title, style: style, handler: { [weak self] _ in

// Callback via action
action?(buttonIndex)

// Callback via observers
if let this = self {
for observer in this.observers {
observer.onNext(buttonIndex)
observer.onCompleted()
}
this.observers = []
}
}) )
return self
}

/**
* Returns an Observable that will emit the pressed button index and complete.
* It's important to keep a reference to the AlertBuilder, otherwise the events won't be received.
*/
func observable() -> Observable<Int> {

return Observable<Int>.create { observer in
self.observers.append(observer)
return Disposables.create()
}
}
}

您可以从 Controller 中使用它,如下所示:

let alert = UIAlertController(title: "title", message: "msg", preferredStyle: .actionSheet)

let builder = AlertBuilder(alert: alert)
.button("no", style: .destructive)
.button("yes")

self.present(alert, animated: true, completion: nil)

self.builder.observable()
.subscribe(onNext: { buttonIndex in /* ... */ })
.disposed(by: bag)

// keep reference to builder so observable() works
self.builder = builder

最佳答案

允许将所有代码保存在一个地方的解决方案是 extension UIAlertViewController:

extension UIAlertController {

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,
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) }
}

}

}

和用法:

let actions: [UIAlertController.AlertAction] = [
.action(title: "no", style: .destructive),
.action(title: "yes")
]

UIAlertController
.present(in: self, title: "Alert", message: "message", style: .alert, actions: actions)
.subscribe(onNext: { buttonIndex in
print(buttonIndex)
})
.disposed(by: bag)

代码和逻辑非常简单,所以我在这里不给你解释。有问题就问。

关于ios - 如何获得 UIAlertController observable(ReactiveCocoa 或 RxSwift)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49538546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com