我试图在我的PasswordResetViewController上使用RACCommand。在我的viewDidLoad中,我有以下内容:

sendButtonItem = UIBarButtonItem(title: "Reset Password", style: .Done, target: self, action: nil)

var emailIsValid = textField.rac_textSignal().map { text -> AnyObject! in
    return count((text as! String)) > 0
}

let passwordResetSignal = RACSignal.createSignal { (subscriber) -> RACDisposable! in
    User.requestPasswordResetForEmailInBackground(self.textField.text) { success, error in
        if success {
            subscriber.sendNext(success)
            subscriber.sendCompleted()
        } else {
            subscriber.sendError(error)
        }
    }

    return nil
}

sendButtonItem.rac_command = RACCommand(enabled: emailIsValid) { (input) in
    return passwordResetSignal
}

sendButtonItem.rac_command.executionSignals.subscribeError { (error) -> Void in
    println("ERROR!")
}

sendButtonItem.rac_command.executionSignals.subscribeNext { (success) -> Void in
    println("SUCCESS!")
}

navigationItem.rightBarButtonItem = sendButtonItem


rightBarButtonItem启用/禁用了我所期望的,但是没有任何下一个或错误事件。

最佳答案

executionSignals是信号信号。换句话说:每次执行命令时,它都会发送命令信号(在您的情况下为passwordResetSignal)作为下一个值。

如果要预订从passwordResetSignal发送的值,请使用switchToLatest

sendButtonItem.rac_command.executionSignals.switchToLatest().subscribeNext { (success) -> Void in
    println("SUCCESS!")
}


errors中有一个单独的信号RACCommand,可用于订阅从执行信号中接收到的错误(注意:错误将作为next值发送):

sendButtonItem.rac_command.errors.subscribeNext { (error) -> Void in
    println("ERROR!")
}

关于ios - RACCommand困惑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29518291/

10-09 16:14