您的位置:首页 > 移动开发 > IOS开发

IOS 实例一 登录注册界面搭建

2016-10-27 20:52 405 查看
创建两个类,一个RegisterViewController为注册类,ViewController为登录界面类,往项目中添加两张图片,用于用户名和密码右边的图片(如果不需要显示右边的图片也可以不要);

在ViewController.m文件中,先来编写登录界面的代码,这里将用户名输入框和密码输入框声明成员变量,方便其他函数中使用这些对象,代码如下:

@interface ViewController ()
{
UITextField *loginText;
UITextField *passwdText;

}
@end


声明一个的常量,定义屏幕的尺寸

#define SCREEN_SIZE   [UIScreen mainScreen].bounds.size


整个ViewController登录界面代码如下

#import "ViewController.h"
#import "RegisterViewController.h"
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
@interface ViewController () { UITextField *loginText; UITextField *passwdText; } @end
@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

loginText = [[UITextField alloc]initWithFrame:CGRectMake(20, 80, SCREEN_SIZE.width-40, 30)];
loginText.borderStyle = UITextBorderStyleRoundedRect;
loginText.placeholder = @"请输入用户名";

UIImageView *loginImage = [[UIImageVi
4000
ew alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
loginImage.image = [UIImage imageNamed:@"bg_icon_bind"];

loginText.leftView = loginImage;
loginText.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:loginText];

passwdText = [[UITextField alloc]initWithFrame:CGRectMake(20, 130, SCREEN_SIZE.width - 40, 30)];
passwdText.borderStyle = UITextBorderStyleRoundedRect;
passwdText.placeholder = @"请输入密码";
passwdText.secureTextEntry = YES;

UIImageView *passImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
passImage.image = [UIImage imageNamed:@"bg_icon_intro"];
passwdText.leftView = passImage;
passwdText.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:passwdText];

//创建登录
UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
loginBtn.frame = CGRectMake(SCREEN_SIZE.width/4 - 50, 180, 100, 30);
[loginBtn setTitle:@"登录" forState:UIControlStateNormal];
loginBtn.layer.masksToBounds = YES;
loginBtn.layer.cornerRadius = 10;
loginBtn.backgroundColor = [UIColor cyanColor];
[loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:loginBtn];

//注册按钮
UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
registerBtn.frame = CGRectMake(SCREEN_SIZE.width/4*3 - 50, 180, 100, 30);
[registerBtn setTitle:@"注册" forState:UIControlStateNormal];
registerBtn.layer.masksToBounds = YES;
registerBtn.layer.cornerRadius = 10;
registerBtn.backgroundColor = [UIColor cyanColor];
[self.view addSubview:registerBtn];
[registerBtn addTarget:self action:@selector(register) forControlEvents:UIControlEventTouchUpInside];

}

//注册
- (void)register{
RegisterViewController *registe = [[RegisterViewController alloc]init];
[self presentViewController:registe animated:true completion:nil];
}

//登录前,先做校验
- (void)login{
if (loginText.text.length == 0) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"登录名不能为空" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
return;
}

if (passwdText.text.length == 0) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
return;
}

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

}

@end


登录界面如下



简单注册代码如下

#import "RegisterViewController.h"
#define SCREEN_SIZE   [UIScreen mainScreen].bounds.size
@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, SCREEN_SIZE.width - 40, 60)];
label.font = [UIFont systemFontOfSize:23];
label.text = @"注册页面";
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];

//返回按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(SCREEN_SIZE.width/2 - 50, 220, 100, 30);
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(backPress) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

}

- (void)backPress{
[self dismissViewControllerAnimated:true completion:nil];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];.
}

@end


注册界面如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios