我刚开始在我的项目中使用ReactiveCocoa
。我正在尝试弄清楚如何在使用UIAlertView
时处理确认请求。
我似乎无法绕过这个。
UIAlertView
并检查选择了哪个按钮索引如何在执行开始之前首先显示UIAlertView?
class MyViewModel: NSObject {
dynamic var myText = ""
var executeTransaction: RACCommand!
override init() {
super.init()
let validSignal = RACObserve(self, "myText").mapAs {
(text: NSString) -> NSNumber in
return text.length >= 10
}.distinctUntilChanged()
self.executeTransaction = RACCommand(enabled: validSignal, signalBlock: {
(any: AnyObject!) -> RACSignal! in
return self.executingTransactionSignal()
})
}
func executingTransactionSignal() -> RACSignal! {
return RACSignal.createSignal {
subscriber in
subscriber.sendNext("testing")
subscriber.sendCompleted()
return nil
}
}
}
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.inputField.rac_textSignal() ~> RAC(self.viewModel, "myText")
self.verifyButton.rac_command = self.viewModel.executeTransaction
let alert = UIAlertView(title: "Title", message: "Continue", delegate: nil, cancelButtonTitle: "Change", otherButtonTitles: "Yes")
let alertSignal = return alert.rac_buttonClickedSignal().map({ (buttonIndex) -> AnyObject! in
let index = buttonIndex as Int
return index == 1
})
// trying see how to show, and subscribe to self.viewModels.executeTransaction.executionSignals
}
}
...更新
我最终做的是在我的视图模型中创建一个接受闭包的方法,并返回RACCommand。我不确定如果这是最优雅的方式。