iOS开发之ReactiveCocoa框架(RAC)第二篇登录实例
2017-07-10 14:08
513 查看
我们平时在做登录注册的时候,需要校验用户名和密码的textField输入内容,比如字符长度,设置textField的deleagte。用来设置Button是否可以被点击。
代码如下:
.h文件
除了设置delegate的方法以外,还能使用RAC的处理方式。利用RAC处理事件很方便,可以把要处理的事情和监听的事情的代码放在一起,这样非常方便我们管理,就不需要跳到对应的方法里。
实例如下:
.h文件
代码如下:
.h文件
#import "LoginViewViewController.h" @interface LoginViewViewController ()<UITextFieldDelegate> @property(nonatomic,strong)UILabel * nameLabel; @property(nonatomic,strong)UITextField * nameTextField; @property(nonatomic,strong)UILabel * passwordLbale; @property(nonatomic,strong)UITextField * passwordTextField; @property(nonatomic,strong)UIButton * loginButton; @end @implementation LoginViewViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor =[UIColor whiteColor]; [self createUI]; [self.nameTextField setDelegate:self]; [self.passwordTextField setDelegate:self]; self.loginButton.enabled = NO; -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSString * str =[textField.text stringByReplacingCharactersInRange:range withString:string]; NSString * s1 =self.nameTextField.text; NSString * s2 =self.passwordTextField.text; if (textField ==self.nameTextField) { s1 = str; }else{ s2 = str; } if (s1.length>0 && s2.length>6) { self.loginButton.enabled = YES; }else{ self.loginButton.enabled = NO; } NSLog(@"%@,%@",s1,s2); return YES; } -(void)createUI{ self.nameLabel =[[UILabel alloc]initWithFrame:CGRectMake(50, 200, 100, 40)]; self.nameLabel.text = @"用户名:"; self.nameLabel.textColor =[UIColor blueColor]; [self.view addSubview:self.nameLabel]; self.nameTextField =[[UITextField alloc]initWithFrame:CGRectMake(120, 200, 200, 40)]; self.nameTextField.backgroundColor =[UIColor grayColor]; [self.view addSubview:self.nameTextField]; self.passwordLbale =[[UILabel alloc]initWithFrame:CGRectMake(50, 260,100,40)]; self.passwordLbale.text =@"密码:"; self.passwordLbale.textColor =[UIColor blueColor]; [self.view addSubview:self.passwordLbale]; self.passwordTextField =[[UITextField alloc]initWithFrame:CGRectMake(120, 260, 200, 40)]; self.passwordTextField.backgroundColor =[UIColor grayColor]; [self.view addSubview:self.passwordTextField]; self.loginButton =[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-100)/2, 400, 100, 50)]; [self.loginButton setTitle:@"登录" forState:UIControlStateNormal]; [self.loginButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [self.view addSubview:self.loginButton]; }
除了设置delegate的方法以外,还能使用RAC的处理方式。利用RAC处理事件很方便,可以把要处理的事情和监听的事情的代码放在一起,这样非常方便我们管理,就不需要跳到对应的方法里。
实例如下:
.h文件
#import "LoginViewViewController.h" @import ReactiveCocoa;//引用RAC @interface LoginViewViewController () @property(nonatomic,strong)UILabel * nameLabel; @property(nonatomic,strong)UITextField * nameTextField; @property(nonatomic,strong)UILabel * passwordLbale; @property(nonatomic,strong)UITextField * passwordTextField; @property(nonatomic,strong)UIButton * loginButton; @end @implementation LoginViewViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor =[UIColor whiteColor]; [self createUI]; //RAC方式 /* // 展示:关于textField组件,RAC团队设置了一个专门取字符串的信号量。它跟使用回调去实现的方式是一样的。 [self.nameTextField.rac_textSignal subscribeNext:^(id x) { NSLog(@"%@",x); }]; */ /* //一个输入框的处理 RACSignal * enableSignal =[self.nameTextField.rac_textSignal map:^id(NSString * value) { return @(value.length > 0); }]; */ RACSignal * enableSignal = [[RACSignal combineLatest:@[self.nameTextField.rac_textSignal,self.passwordTextField.rac_textSignal]] map:^id(id value) { NSLog(@"%@",value); return @([value[0] length]>0 && [value[1] length] >6); }]; self.loginButton.rac_command =[[RACCommand alloc]initWithEnabled:enableSignal signalBlock:^RACSignal *(id input) { return [RACSignal empty]; }]; }
相关文章推荐
- iOS开发之ReactiveCocoa框架(RAC)第三篇实例颜色选择器
- iOS开发之ReactiveCocoa框架(RAC)第四篇构建一个信号量
- iOS开发之ReactiveCocoa框架(RAC)第五篇队列与高级函数
- iOS开发之ReactiveCocoa框架(RAC)第六篇程序定位
- ReactiveCocoa - iOS开发的新框架
- ReactiveCocoa - iOS开发的新框架
- ReactiveCocoa - iOS开发的新框架
- ReactiveCocoa - iOS开发的开源框架
- ReactiveCocoa - iOS开发的新框架
- ReactiveCocoa - iOS开发的新框架
- iOS开发之ReactiveCocoa下的MVVM
- iOS Reactivecocoa(RAC)知其所以然(源码分析,一篇足以)
- iOS开发之ReactiveCocoa下的MVVM(干货分享)
- 【iOS】小项目框架设计(ReactiveCocoa+MVVM+AFNetworking+FMDB)
- iOS开发之ReactiveCocoa下的MVVM(干货分享)
- iOS开发进阶 - 日志输出框架CocoaLumberjack与XcodeColors插件的简单使用(swift版)
- iOS开发之Cocoa框架详解
- 【iOS】【ReactiveCocoa】[RACSignal interval]定时器
- 【iOS开发】从Cocoa框架说开去--Fundation框架系列
- ReactiveCocoa框架菜鸟入门——信号(Signal)详解 第五课:适合给新手看的RAC用法总结