美文网首页
ReactiveObjC登录简单使用

ReactiveObjC登录简单使用

作者: 文子飞_ | 来源:发表于2020-11-21 01:51 被阅读0次

    ViewModel

    @interface HomeVM : NSObject
    
    @property (nonatomic, copy) NSString *phoneNumber;
    @property (nonatomic, copy) NSString *pwd;
    @property (nonatomic, copy) NSString *verifyCode;
    
    @property (nonatomic, assign) BOOL isEnableVerifyButton;
    @property (nonatomic, assign) BOOL isEnableNextButton;
    
    @property (nonatomic, assign) BooL isAgreeProtocol;
    @property (nonatomic, strong) RACSignal *registerEnableSignal;
    
    @end
    
    @implementation HomeVM
    
    - (instancetype)init {
        self = [super init];
        if (self) {
            
            [self setRAC];
            
        }
        return self;
    }
    
    
    - (void)setRAC {
        
        // 绑定 isEnableVerifyButton 可用条件
        RAC(self, isEnableVerifyButton) = [RACSignal combineLatest:@[RACObserve(self, phoneNumber)] reduce:^( NSString *phoneNumberStr){
            return @(phoneNumberStr.length == 13);
        }];
        
        //绑定 isEnableNextButton 可用条件
        RAC(self, isEnableNextButton) = [RACSignal combineLatest:@[RACObserve(self, phoneNumber), RACObserve(self, verifyCode), RACObserve(self, isAgreeProtocol)] reduce:^(NSString *phoneNumberStr, NSString *verifyCodeStr, NSNumber *isAgree){
            return @(isAgree.boolValue && phoneNumberStr.length == 13 && verifyCodeStr.length == 6);
        }];
         
    }
    
    
    @end
    

    UIView

    @interface HomeView : UIView
    
    @property (nonatomic, strong) UIButton *verifyButton;
    @property (nonatomic, strong) UIButton *loginButton;
    @property (nonatomic, strong) UIButton *isReadButton;
    
    @property (nonatomic, strong) UITextField *verifyField;
    @property (nonatomic, strong) UITextField *phoneField;
    
    @end
    

    控制器

    @implementation HomeVC
    - (void)setRAC {
        [super setRAC];
        
        [self buttonEnable];
        [self phoneNumberCodeLimit];
    }
    
    - (void)buttonEnable {
        
        [[RACSignal merge:@[self.homeView.phoneField.rac_textSignal, RACObserve(self.homeView.phoneField, text)]] subscribeNext:^(id  _Nullable x) {
            self.homeVM.phoneNumber = self.homeView.phoneField.text;
        }];
        [[RACSignal merge:@[self.homeView.verifyField.rac_textSignal, RACObserve(self.homeView.verifyField, text)]] subscribeNext:^(id  _Nullable x) {
            self.homeVM.verifyCode = self.homeView.verifyField.text;
        }];
        
      // 勾选 阅读协议
      RAC(self.homeVM, isAgreeProtocol) = [RACSignal merge:@[RACObserve(self.homeView.isReadButton, selected)]];
    
      RAC(self.homeView.verifyButton, enabled) = RACObserve(self.homeVM, isEnableVerifyButton);
      //RAC(self.homeView.loginButton, enabled) = RACObserve(self.homeVM, isEnableNextButton);
      // 立即注册按钮
      RAC(self.homeView.loginButton, enabled) = self.homeVM.registerEnableSignal;
    }
    
    - (void)phoneNumberCodeLimit {
        
        __weak HomeVC *weakSelf = self;
        [self.homeView.phoneField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            if (x.length > 13) {
                x = [x substringToIndex:x.length -1];
                weakSelf.homeView.phoneField.text = x;
                NSLog(@"手机号不能超过13位");
            }
        }];
        
        [self.homeView.verifyField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            if (x.length > 6) {
                x = [x substringToIndex:x.length - 1];
                weakSelf.homeView.verifyField.text = x;
                NSLog(@"验证码不能超过6位");
            }
        }];
        
        [[self.homeView.verifyButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSLog(@"x = %@", x);
        }];
        
        [[self.homeView.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSLog(@"x = %@", x);
        }];
        
    }
    @end
    

    相关文章

      网友评论

          本文标题:ReactiveObjC登录简单使用

          本文链接:https://www.haomeiwen.com/subject/ctlsiktx.html