Skip to content

Instantly share code, notes, and snippets.

@SunXiaoShan
Last active July 8, 2017 19:42
Show Gist options
  • Save SunXiaoShan/c78900272ad554cca2a54027cfb40d7d to your computer and use it in GitHub Desktop.
Save SunXiaoShan/c78900272ad554cca2a54027cfb40d7d to your computer and use it in GitHub Desktop.
RAC_SimpleDemo
@interface Model:NSObject
@property (strong, nonatomic) NSString *content;
@end
@implementation Model
@end
@interface ViewModel:NSObject
@property (strong, nonatomic) NSString *content;
- (void) execute:(NSString *)content;
@end
@implementation ViewModel
- (void) execute:(NSString *)content {
// Step 5 : execute function
[[RACScheduler mainThreadScheduler] afterDelay:3 schedule:^{
Model *result = [Model new];
result.content = @"result";
// Step 6 : assign new value
self.content = result.content;
}];
}
@end
@interface View:UIViewController
@property (weak, nonatomic) IBOutlet UILabel *labelView;
@property (weak, nonatomic) IBOutlet UITextField *textView;
@property (weak, nonatomic) IBOutlet UIButton *btnButton;
@property (strong, nonatomic) ViewModel *viewModel;
@end
@implementation View
- (void) viewDidLoad {
// Step 1 : assign view model
_viewModel = [ViewModel new];
// Step 2 : set button action
[[self.btnButton rac_signalForControlEvents:UIControlEventTouchUpInside]
subscribeNext:^(__kindof UIControl * _Nullable x) {
// Step 4 : set text & execute
[_viewModel execute:self.textView.text];
}];
// Step 3 : Regist observer
[RACObserve(_viewModel, content) subscribeNext:^(id x) {
// Step 7 : Refresh UIView
[self.labelView setText:x];
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment