您的位置:首页 > Web前端 > React

iOS开发之ReactiveCocoa框架(RAC)第二篇登录实例

2017-07-10 14:08 513 查看
我们平时在做登录注册的时候,需要校验用户名和密码的textField输入内容,比如字符长度,设置textField的deleagte。用来设置Button是否可以被点击。

代码如下:

.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 ios开发 框架