登录RAC

在viewmodel中

- (instancetype)init{    
self = [super init];   
 if (!self) {
        return nil;    } 
   @weakify(self)   
 self.loginCommand = [[RACCommand alloc]initWithEnabled:[self validateLoginInputs]                                               signalBlock:^RACSignal *(id input) { 
       @strongify(self)   
     return [self signInWithUsername:self.userName password:self.passWord];  
              }]; 
   return self; }

/* ** * 校验btn可点的方法 */

- (RACSignal *)validateLoginInputs{ 
   RACSignal *sig = [RACSignal combineLatest:@[RACObserve(self, userName),RACObserve(self, passWord)] reduce:^id{      return @([self checkUserName:_userName] && _passWord.length >=6);   
 }]; 
   return sig;}
- (BOOL )checkUserName:(NSString *)userName{    US2Condition *condition = [[US2Condition alloc] initWithRegexString:@"1[0-9]{10}"]; 
   if ([condition check:userName]) {    
    return YES; 
   }  
  return NO;
}

/* ** * 点击按钮时会触发的方法 */

    - (RACSignal *)signInWithUsername:(NSString *)username password:(NSString *)password{  
      return [[[RACSignal createSignal:^RACDisposable *(idsubscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLAuthenticationChallenge * _Nonnull challenge, NSURLCredential *__autoreleasing  _Nullable * _Nullable credential) {
if (challenge.previousFailureCount == 0) {NSURLCredential *mycredential = [[NSURLCredential alloc]initWithUser:username password:password persistence:NSURLCredentialPersistenceForSession];
*credential = mycredential;
return NSURLSessionAuthChallengeUseCredential;}else{return NSURLSessionAuthChallengeCancelAuthenticationChallenge;}}];
NSDictionary *parameters = @{};[manager POST:@"xxxx" parameters:parameterssuccess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {                           NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.userName forKey:@"userName"];[defaults setObject:self.passWord forKey:@"passWord"];[defaults synchronize];
[subscriber sendNext:responseObject];
} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
[subscriber sendError:error];
}];
});
return nil;
}]publish]autoconnect];}

在controller中使用先绑定信号----

           - (void)viewDidLoad {
[super viewDidLoad];
[self setBackGroundColorForTextfield];
}

// 处理text的背景颜色

 - (void)setBackGroundColorForTextfield{

    self.loginbtn = [[DeformationButton alloc]initWithFrame:CGRectMake(130, 350, 100, 50) withColor:[UIColor redColor]] ;
[self.loginbtn.forDisplayButton setTitle:@"登陆" forState:UIControlStateNormal];[self.loginbtn.forDisplayButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
[self.loginbtn.forDisplayButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];[self.loginbtn.forDisplayButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
[self.view addSubview:self.loginbtn];
/**

*与modelview进行绑定

*/

   [self.userName.rac_textSignal subscribeNext:^(NSString *text) {self.loginmodel.userName = text;}];

  [self.passWord.rac_textSignal subscribeNext:^(NSString *x) {self.loginmodel.passWord = x;}];self.loginbtn.rac_command = self.loginmodel.loginCommand;

/**

*rac 使用代理协议修改placeholder

*/

   self.userName.delegate = self;

   RACSignal *sigBegin = [self rac_signalForSelector:@selector(textFieldDidBeginEditing:) fromProtocol:@protocol(UITextFieldDelegate)];

  RACSignal *sigEnd = [self rac_signalForSelector:@selector(textFieldDidEndEditing:) fromProtocol:@protocol(UITextFieldDelegate)];

  RAC(self.userName, placeholder) = [[RACSignal merge:@[sigBegin, sigEnd]]map:^id(UITextField *value) {return self.userName.editing?@"手机号":@"请输入手机号";}];

   self.passWord.delegate = self;

    RACSignal *sig_Begin = [self rac_signalForSelector:@selector(textFieldDidBeginEditing:) fromProtocol:@protocol(UITextFieldDelegate)];

   RACSignal *sig_End = [self rac_signalForSelector:@selector(textFieldDidEndEditing:) fromProtocol:@protocol(UITextFieldDelegate)];

   RAC(self.passWord, placeholder) = [[RACSignal merge:@[sig_Begin, sig_End]]map:^id(UITextField *value) {

   return self.passWord.editing?@"密码":@"请输入密码";

    }];

/**

*监听并修改textfield的背景色

*/

   RAC(self.userName,backgroundColor) = [self.userName.rac_textSignal map:^(NSString *text) {

   return text.length == 11? [UIColor whiteColor]:[UIColor lightGrayColor];

      }];

         RAC(self.passWord,backgroundColor) =     [self.passWord.rac_textSignal map:^(NSString *text) {

       return text.length >= 6? [UIColor whiteColor]:[UIColor lightGrayColor];

     }];

/**

*当点击按钮的时候textfiled 不能编辑

*/

[self.loginbtn.rac_command.executing subscribeNext:^(NSNumber *x) {

NSLog(@"%@-",x);

if ([x boolValue]) {

self.userName.userInteractionEnabled = NO;

self.passWord.userInteractionEnabled = NO;

}else{

self.userName.userInteractionEnabled = YES;

self.passWord.userInteractionEnabled = YES;

}

}];

[self.loginbtn.rac_command.executionSignals.flatten subscribeNext:^(id isOk) {

NSLog(@"%@-----",isOk);

UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

Ex_ViewController *ex = [story instantiateViewControllerWithIdentifier:@"ex_viewController"];

[self presentViewController:ex animated:YES completion:nil];

}];

[self.loginbtn.rac_command.errors subscribeNext:^(id isOk) {

NSLog(@"%@,",isOk);

[self setAlertView];

}];

}
- (void)setAlertView{

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入的手机号或密码有误" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

self.loginbtn.isLoading = NO;

}];

[alertVC addAction:action];

[self presentViewController:alertVC animated:YES completion:nil];

}

对于这种编程还在学习中,会不断更新----

你可能感兴趣的:(登录RAC)