iOS 回调只会用Block和Delegate?

2021-12-16 09:27:23 浏览数 (1)

写一个View,View上放一个按钮,如果按钮点击后要调用Controller的方法可以用Block或者委托实现。除了Block和委托外还可以使用ReactiveObjC

定义一个View
代码语言:javascript复制
#import <UIKit/UIKit.h>
#import <ReactiveObjC/RACSignal.h>

NS_ASSUME_NONNULL_BEGIN

@interface RACView : UIView

@property (nonatomic,strong) RACSubject *btnClickSingle;

@end


NS_ASSUME_NONNULL_END
代码语言:javascript复制
#import "RACView.h"
#import <ReactiveObjC/RACSubject.h>
#import <ReactiveObjC/UIControl RACSignalSupport.h>

@implementation RACView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        //创建一个按钮
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(40, 80, frame.size.width-80, 70)];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 8;
        btn.layer.borderWidth = 1;
        [btn setTitle:@"点击我" forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor blueColor].CGColor;
        [self addSubview:btn];
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            //发送信号
            [self.btnClickSingle sendNext:@{@"value":@"按钮点击了"}];
            
        }];
    }
    return self;
}

-(RACSubject *)btnClickSingle{
    if (!_btnClickSingle) {
        _btnClickSingle = [RACSubject subject];
    }
    return _btnClickSingle;
}

@end
Controller调用
代码语言:javascript复制
#import "ViewController.h"
#import "RACView.h"
#import <ReactiveObjC/RACSubject.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self demo];
}

-(void)demo{
    RACView *racView = [[RACView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    [self.view addSubview:racView];
    [racView.btnClickSingle subscribeNext:^(NSDictionary  * _Nullable x) {
        //要传多个值,,可以传一个集合
        NSLog(@"%@",[x objectForKey:@"value"]);
    }];
}

所以使用ReactiveObjC也是非常方便的,下面就简单介绍ReactiveObjC使用方法

代替监听事件方法(按钮点击)
代码语言:javascript复制
//创建一个按钮
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 70, 70)];
btn.backgroundColor = [UIColor redColor];
[self.view addSubview:btn];
//监听点击事件
[[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    NSLog(@"按钮点击了%@",x);
}];
代替KVO
代码语言:javascript复制
//创建一个按钮
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 70, 70)];
btn.backgroundColor = [UIColor redColor];
[self.view addSubview:btn];
//监听点击事件
[[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
    //改变btn的Frame
    x.frame = CGRectMake(100,100,200, 200);
}];
[[btn rac_valuesAndChangesForKeyPath:@"frame" options:(NSKeyValueObservingOptionNew) observer:self] subscribeNext:^(RACTwoTuple<id,NSDictionary *> * _Nullable x) {
    //RACTwoTuple是一个集合类型,可以用数组的方式获取到里面的内容。
    NSLog(@"frame改变了%@",x.second);
}];
代替通知
代码语言:javascript复制
//创建一个文本输入框
UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 200, 50)];
field.backgroundColor = [UIColor grayColor];
[self.view addSubview:field];

// 监听键盘弹出事件
[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] takeUntil:[self rac_willDeallocSignal]] subscribeNext:^(NSNotification * _Nullable x) {
    NSLog(@"%@", x);
}];
监听文本输入框文字的变化
代码语言:javascript复制
 //创建一个文本输入框
    UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 200, 50)];
    field.backgroundColor = [UIColor grayColor];
    [self.view addSubview:field];
    [field.rac_textSignal subscribeNext:^(id x) {
        NSLog(@"文字改变了%@",x);
    }];
代替timer循环执行某个方法
代码语言:javascript复制
[[RACSignal interval:1.0 onScheduler:[RACScheduler scheduler]] subscribeNext:^(NSDate * _Nullable x) {
        NSLog(@"%@",x);
    }];
同时接受多个信号
代码语言:javascript复制
-(void)demo{
    // 处理多个请求,都返回结果的时候,统一做处理.
    RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        // 发送请求1
        [subscriber sendNext:@"发送请求1"];
        return nil;
    }];
    
    RACSignal *request2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        // 发送请求2
        [subscriber sendNext:@"发送请求2"];
        return nil;
    }];
    
    // 使用注意:几个信号,selector的方法就几个参数,每个参数对应信号发出的数据。
    // 不需要订阅:不需要主动订阅,内部会主动订阅
    [self rac_liftSelector:@selector(updateUIWithR1:r2:) withSignalsFromArray:@[request1,request2]];
    
}
// 更新UI
- (void)updateUIWithR1:(id)data r2:(id)data1
{
    NSLog(@"更新UI%@ %@",data,data1);
}

0 人点赞