学以致用,
有的时候学习了很多理论
却还是忘了实践
OC 中代替代理 简洁编程
#import "ViewController.h"
#import <ReactiveObjC.h>
#import "SKView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
SKView *vw = [[SKView alloc]initWithFrame:CGRectMake(50, 200, 100, 100)];
// 创建一个subject 赋值给view 的内部
RACSubject *subject = [RACSubject subject];
// 接受信号
[subject subscribeNext:^(id _Nullable x) {
NSLog(@"点击了事件");
}];
vw.subject = subject;
vw.backgroundColor = [UIColor redColor];
[self.view addSubview:vw];
}
@interface SKView : UIView
@property(nonatomic,strong)RACSubject *subject;
@end
#import "SKView.h"
@implementation SKView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.subject sendNext:@"我被点击了"];
}
@end