ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { //登录页面的组成 //登陆名提示 输入框 //密码提示 //登录按钮 //注册按钮 //作为私有 UILabel* _lbUserName; UILabel* _lbPassword; UITextField* _tfUserName; UITextField* _tfPassWord; UIButton* _btLogin; UIButton* _btRegister; } @end
viewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _lbUserName=[[UILabel alloc]init]; _lbUserName.frame=CGRectMake(20, 60, 80, 40); _lbUserName.text=@"用户名"; _lbUserName.font=[UIFont systemFontOfSize:20]; _lbUserName.textAlignment=NSTextAlignmentLeft; _lbPassword=[[UILabel alloc]init]; _lbPassword.frame=CGRectMake(20, 140, 80, 40); _lbPassword.text=@"密码"; _lbPassword.font=[UIFont systemFontOfSize:20]; _lbPassword.textAlignment=NSTextAlignmentLeft; _tfUserName=[[UITextField alloc]init]; _tfUserName.frame=CGRectMake(120, 60, 180, 40); _tfUserName.placeholder=@"亲输入用户名"; _tfUserName.borderStyle=UITextBorderStyleRoundedRect; _tfPassWord=[[UITextField alloc]init]; _tfPassWord.frame=CGRectMake(120, 140, 180, 40); _tfPassWord.placeholder=@"请输入密码"; _tfPassWord.borderStyle=UITextBorderStyleRoundedRect; _tfPassWord.secureTextEntry=YES; _btLogin=[UIButton buttonWithType:UIButtonTypeRoundedRect]; _btLogin.frame=CGRectMake(120, 300, 80, 40); [_btLogin setTitle:@"登陆" forState:UIControlStateNormal]; [_btLogin addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside]; _btRegister=[UIButton buttonWithType:UIButtonTypeRoundedRect]; _btRegister.frame=CGRectMake(120, 360, 80, 40); [_btRegister setTitle:@"注册" forState:UIControlStateNormal]; [_btRegister addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_lbUserName]; [self.view addSubview:_lbPassword]; [self.view addSubview:_tfUserName]; [self.view addSubview:_tfPassWord]; [self.view addSubview:_btLogin]; [self.view addSubview:_btRegister]; } //登陆 -(void)pressLogin { NSString* strName=@"michael"; NSString* strPass=@"123456"; //获取输入框中的用户名 NSString* strTextName=_tfUserName.text; NSString* strTextPass=_tfPassWord.text; if ([strName isEqualToString:strTextName] && [strPass isEqualToString:strTextPass]) { NSLog(@"用户名密码正确"); // UIAlertView* alView =[[UIAlertView alloc] initWithTitle:@"提示" message:@"验证成功,登陆成功" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil]; // [alView show]; }else { NSLog(@"用户名密码错误"); // UIAlertView* alView=[[UIAlertView alloc] initWithTitle:@"提示" message:@"验证失败 用户名或密码错误" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil]; // [alView show]; } } //注册 -(void)pressRegister { } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //回收键盘 [_tfUserName resignFirstResponder]; [_tfPassWord resignFirstResponder]; } @end