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

我们平时在做登录注册的时候,需要校验用户名和密码的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];
    }];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值