我正在使用UIViewcontroller开发RXSwift的alertmessage扩展。

    // UIViewController+Alertmessage
        public func observeAlertMessageSignal(alert: Observable<String>) {
            let disposeBag = DisposeBag()

            alert.observeOn(MainScheduler.instance)
                .subscribe(onNext:  { message in
                    self.alertCustomTransitionDelegate = CustomTransitioningDelegate()

                    let viewModel = AlertViewModel(message: message)

                    let storyboard = UIStoryboard(name: "Alert", bundle: nil)
                    let viewController = storyboard.instantiateInitialViewController() as! AlertViewController
                    viewController.viewModel = viewModel
                    viewController.delegate = self
                    viewController.modalPresentationStyle = .custom
                    viewController.transitioningDelegate = self.alertCustomTransitionDelegate

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

            }).addDisposableTo(disposeBag)
        }

    // ViewModel

    --> Declaration of alertObservable in viewModel
        var alertObservable = PublishSubject<String>()

    --> usage of observable:
    transportResponse = active.asObservable()
                .filter { $0 }
                .flatMap {  _  in
                    return webservice.loadTransport()
                }
                .catchError { [weak self] e in
                    if let moyaError = e as? MoyaError {
                        if case .underlying(let error) = moyaError {
                            let formattedError = error as NSError
                            let message = NSError.localizedDescription(error: formattedError)
                            self?.alertObservable.onNext(message)
                        }
                    }
                    self?.isLoadingObservable.onNext(false)
                    throw e
                }
                .map { response in
                    return [TransportData(items: response)]
            }

//ViewController:
--> code inside viewDidLoad
        self.observeAlertMessageSignal(alert: viewModel.alertObservable)

调用了observeAlertMessageSignal函数,但没有显示我的AlertViewController。有什么帮助吗?
谢谢!

最佳答案

您将disposeBag声明为方法变量。当方法调用结束时,disposebag将被释放,您的订阅也将被释放。您只需将disposebag传递给方法,并在VC中保存该disposebag

关于ios - UIViewController扩展,用于使用RXSwift发出警报消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42386960/

10-11 19:47