CocoaAction与RxSwift和UIAlertController

时间:2016-10-19 09:40:59

标签: ios uialertcontroller rx-swift

我尝试使用文本字段和按钮实现当前行为: 1 - 文本字段应该不是实时验证,但只有在点击按钮后才必须显示验证错误的错误标签 2 - 如果文本字段是有效的,我必须显示uialertcontroller以取消或继续操作

我特别尝试使用以下代码的第二部分,但它只是第一次工作,如果我点击取消例如我再次点击它看起来像禁用的按钮....不再允许点击。< / p>

            let action = CocoaAction {
            return Observable.create {
                [weak self] observer -> Disposable in
                let alertController = self.getAlertController()
                let ok = UIAlertAction.Action(NSLocalizedString("OK_BUTTON", comment: "OK_BUTTON"), style: .Default)
                ok.rx_action = CocoaAction { _ in
                    return self!.viewModel!.modify(self?.addressTextFiled.rx_text)
                        .doOnNext({ data in
                            if let data = data
                            {
                                self!.showMessage(data.message)
                            }
                        })
                        .map { _ in Void() }
                }
                let cancelAction = UIAlertAction(title: NSLocalizedString("CANCEL_BUTTON", comment: "CANCEL_BUTTON"), style: .Cancel) { (_) in }

                alertController.addAction(ok)
                alertController.addAction(cancelAction)

                self!.presentViewController(alertController, animated: true, completion: nil)
                return NopDisposable.instance
            }
        }
        confirmButton.rx_action = action

第一点你有什么建议吗?

感谢帮助我!!

1 个答案:

答案 0 :(得分:0)

在viewModel中,返回的observable是:

    func modify() -> Observable<StatusResponse?>
{
    return input!.continueClick
        .withLatestFrom(requestData!)
        .flatMap{ [unowned self] (code, mail) -> Observable<StatusResponse?> in
            return self.provider.request(APIProvider.ModifyRequest(code, "A", mail))
                .mapObjectOptional(ModifyStatusResponse.self)
                .trackActivity(self.activityIndicator)
        }
        .retry()
        .shareReplay(1)
}

什么可以避免完成观察?