如何使用RACCommand处理确认

时间:2015-02-16 05:15:51

标签: ios objective-c swift mvvm reactive-cocoa

我刚开始在我的项目中使用ReactiveCocoa。我正在尝试弄清楚如何在使用UIAlertView时处理确认请求。

我似乎无法绕过这个。

  1. 启用按钮并进行验证
  2. 显示UIAlertView并检查选择了哪个按钮索引
  3. 如果是,请继续从服务器获取数据,如果不执行任何操作。
  4. 如何在执行开始之前首先显示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。我不确定如果这是最优雅的方式。

0 个答案:

没有答案