1.登录界面的构成
2.登录界面的控件创建
3.登录界面的功能实现
ViewController.h文件声明:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
//登录界面的组成:
//用户名提示(用label):输入框(UITextField)
//密码提示(用label):密码输入框(UITextField)
//登录按钮
//注册按钮 这两个用UIButton
//用户名提示label
UILabel * _lbUserName;
//密码提示label
UILabel * _lbPassWord;
//用户名输入框
UITextField * _tfUserName;
//密码输入框
UITextField * _tfPassWord;
//登录按钮
UIButton * _btLogin;
//注册按钮
UIButton * _btRegister;
}
ViewController.m文件的使用
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//用户名提示标签创建
_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;
//登录和注册按钮btn的创建
_btLogin = [UIButton buttonWithType:UITextBorderStyleRoundedRect];
_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:_tfPassWord];
[self.view addSubview:_tfUserName];
[self.view addSubview:_btRegister];
[self.view addSubview:_btLogin];
}
//登录事件函数
- (void)pressLogin
{
NSString * strName = @"michael";
NSString * strPass = @"123456";
//获取输入框中的用户名文字 和 密码文字
NSString * strTextName = _tfUserName.text;
NSString* strTextPass = _tfPassWord.text;
if ( [strName isEqualToString:strTextName] && [strPass isEqualToString:strTextPass] ){
NSLog(@"用户名和密码均正确");
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleActionSheet];
[self presentViewController:actionSheet animated:YES completion:nil];
}else{
NSLog(@"验证失败");
UIAlertController *actionSheet1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"验证失败 用户名或密码错误" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了按钮1,进入按钮1的事件");
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消");
}];
[actionSheet1 addAction:action1];
[actionSheet1 addAction:action2];
[self presentViewController:actionSheet1 animated:YES completion:nil];
}
}
//注册事件函数
- (void)pressRegister
{
}
//点击任意地方 收回键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//回收键盘
[_tfUserName resignFirstResponder];
[_tfPassWord resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end