textfield 登录页面Number&Password的同时判定

来源:互联网 发布:湘北vs陵南数据 编辑:程序博客网 时间:2024/05/02 01:58

//

//  CONE.m

//  TableView

//

//  Created by 李小亮 on 16/6/8.

//  Copyright © 2016李小亮. All rights reserved.

//


#import "CONE.h"


@interface CONE ()


{

    UITextField*number;

    UITextField*password;

    UIButton*button;

    UILabel*label2;

    UILabel*label3;

}




@end


@implementation CONE


- (void)viewDidLoad {

    [superviewDidLoad];

    self.title=@"首页";

    //创建文本

    UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(20, 250, 70, 40)];

    label.text=@"账号:";

    label.font=[UIFontsystemFontOfSize:25];

    [self.viewaddSubview:label];

    

    UILabel*label1=[[UILabelalloc]initWithFrame:CGRectMake(20, 330, 70, 40)];

    label1.text=@"密码:";

    label1.font=[UIFontsystemFontOfSize:25];

    [self.viewaddSubview:label1];

    

    //创建logo图片

    UIImageView*imageView=[[UIImageViewalloc]init];

    imageView.frame=CGRectMake(140, 100, 120, 120);

    imageView.backgroundColor=[UIColorclearColor];

    UIImage*image=[UIImageimageNamed:@"a.png"];

    imageView.image=image;

    imageView.layer.masksToBounds=YES;

    imageView.layer.cornerRadius=60;

    [self.viewaddSubview:imageView];

    

 

    

    //创建帐号textfeild

    number=[[UITextFieldalloc]initWithFrame:CGRectMake(85, 250, 250, 40)];

    number.borderStyle=UITextBorderStyleRoundedRect;

    number.backgroundColor=[UIColorgrayColor];

    number.placeholder=@"请输入账号";

    number.font=[UIFontfontWithName:@"正常"size:18.0f];

    number.clearButtonMode=UITextFieldViewModeAlways;

    number.keyboardType=UIKeyboardTypeNumbersAndPunctuation;

    number.delegate=self;//必须在.h中声明,.h写上<UITextFieldDelegate>

    [self.viewaddSubview:number];

    

    

    //创建密码textfield

    password=[[UITextFieldalloc]initWithFrame:CGRectMake(85, 330, 250, 40)];

    password.borderStyle=UITextBorderStyleRoundedRect;

    password.backgroundColor=[UIColorgrayColor];

    password.placeholder=@"请输入密码";

    password.font=[UIFontfontWithName:@"正常"size:18.0f];

    password.clearButtonMode=UITextFieldViewModeAlways;

    password.keyboardType=UIKeyboardTypeNumberPad;

    password.delegate=self;//必须在.h中声明,.h写上<UITextFieldDelegate>

    [self.viewaddSubview:password];

    

    //创建按钮

    button=[UIButtonbuttonWithType:UIButtonTypeSystem];

    button.frame=CGRectMake(20, 420, 320, 40);

    button.backgroundColor=[UIColorgrayColor];

    [buttonsetTitle:@"登录"forState:UIControlStateNormal];

    [buttonsetTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    button.layer.masksToBounds=YES;

    button.layer.cornerRadius=6;

    

    [buttonaddTarget:selfaction:@selector(ButtonClicked)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

   

    //监听键盘升起时的通知

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardwillshow)name:UIKeyboardWillShowNotificationobject:nil];

    

    //监听键盘隐藏时的通知

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardwillhide)name:UIKeyboardWillHideNotificationobject:nil];

    

    

}


-(void)keyboardwillshow//键盘展示时,触发此事件

{

    

    [UIViewanimateWithDuration:0.25animations:^{

        button.frame=CGRectMake(20, 410, 320, 40);}completion:^(BOOL finished) {

            

        }];

    

}


-(void)keyboardwillhide////键盘隐藏时,触发此事件

{

    

    [UIViewanimateWithDuration:0.25animations:^{

        button.frame=CGRectMake(20, 420 , 320, 40);}completion:^(BOOL finished) {

            

        }];

    

}



//按钮点击事件

-(void)ButtonClicked{

    

    if ([number.textisEqualToString:@"李亮"] &&[password.textisEqualToString:@"15991726153"]) {

        NSLog(@"登录成功");

        

        UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"提示"message:@"登录成功"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

        alertView.tag=1;

        [alertView show];

        

    }

    

    else {

        NSLog(@"登录失败");

        UIAlertView *alertView1 = [[UIAlertViewalloc] initWithTitle:@"失败"message:@"消除文本框里的内容?"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

        alertView1.tag=2;

        [alertView1 show];

    }

    

    NSLog(@"123");

}



#pragma mark ----提示框的代理方法

//根据被点击按钮的索引处理点击事件

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    

    if (alertView.tag==1) {

        NSLog(@"您点击的是成功的按钮");

        if (buttonIndex==0) {

            NSLog(@"您点击的是成功里的取消");

        }else{

            NSLog(@"您点击的是成功里的确认");

        }

        

    }elseif (alertView.tag==2){

        NSLog(@"您点击的是失败的");

        if (buttonIndex==0) {

            NSLog(@"您点击的是失败里的取消");

        }else{

            NSLog(@"您点击的是失败里的确认");

            number.text=nil;

            password.text=nil;

            

        }

        

    }

}






#pragma mark ----textfield的代理方法

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    

    returnYES;

    

}

//进入编辑模式时调用

-(void)textFieldDidBeginEditing:(UITextField *)textField


{

    NSLog(@"进入编辑模式");

    

}

//textfield是否可以结束编辑

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField


{

    

    returnYES;//如果将return设置成NO,无法结束textfield的输入,点击其他textfield就无法输入内容

    

}


//结束编辑时调用

//    -(void)textFieldDidEndEditing:(UITextField*)textField

//    {

//        NSLog(@"内容结束:%@",textField.text);

//        if (textField.text.length!=11) {

//            NSLog(@"您输入的内容太少了请重新输入");

//            textField.text=nil;

//        }

//

//    }


//是否可以点击清除按钮

-(BOOL)textFiledShouldClear:(UITextField*)textfield


{

    

    returnYES;

    

}



//点击空白处,收起键盘


-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event


{

    [passwordresignFirstResponder];

    

    [numberresignFirstResponder];

    

}

//是否可以点击return按钮

//-(BOOL)textFieldShouldReturn:(UITextField*)textField

//{

//点击return键盘收起

//[textField resignFirstResponder];


//return YES;


//}







- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end

0 0