Last active
July 8, 2017 19:42
-
-
Save SunXiaoShan/c78900272ad554cca2a54027cfb40d7d to your computer and use it in GitHub Desktop.
RAC_SimpleDemo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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