Search code examples
iosswiftreactive-cocoa

Enable/disable UIButton according to number of characters in UITextField in ReactiveCocoa 4


I want to set the enabled property of a UIButton to true iff there is at least one character in a UITextField. How can this be achieved in Reactive Cocoa 4?


Solution

  • You can do it like this:

    NSNotificationCenter.defaultCenter()
      .rac_notifications(UITextFieldTextDidChangeNotification, object: textField)
       .map { $0.object as! UITextField }
       .map { $0.text }
       .map { $0.isEmpty }
       .startWithNext { button.enabled = !$0 }