I want to monitor the UIButton.enabled property to change the button.titleColor
I have done in OC like this:
#import "ViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
@interface ViewController () <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initUI];
}
- (void)initUI {
__weak typeof(self) weakSelf = self;
[[RACObserve(self.btn, enabled) map:^id(id value) {
return [value boolValue] ? [UIColor greenColor] : [UIColor redColor];
}] subscribeNext:^(id x) {
[weakSelf.btn setTitleColor:x forState:UIControlStateNormal];
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.btn.enabled = !self.btn.enabled;
}
@end
Now, i try to achieve the same goal in swift use the lastest Version of ReactiveCocoa, how to do?
ReactiveCocoa 5.0 will add UIKit Extensions.
With
button.reactive.values(forKeyPath:)This works for me in a simple Playground:
However, this is not encouraged because
With a model
In this example a simple
MutablePropertyis used as model, this could be in a ViewModel or whereverUnfortunaetly you can not bind directly to the buttons title color as you can with
isEnabled