第一步:创建视图控制器并创建LoginViewController、RegistViewController、PasswordViewController三个控制器:
#import "MainViewController.h"
#import "LoginViewController.h"
#import "RegistViewController.h"
#import "PasswordViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
RegistViewController *registVC=[[RegistViewController alloc]init];
[self.view addSubview:registVC.view];
PasswordViewController *passwordVC=[[PasswordViewController alloc]init];
[self.view addSubview:passwordVC.view];
LoginViewController *logicaVC = [[LoginViewController alloc] init];
[self.view addSubview:logicaVC.view];
}
第二步:创建LTView视图:
1、声明属性:
#import <UIKit/UIKit.h>
@interface LTView : UIView
@property(nonatomic,retain)UILabel *titleLabel;
@property(nonatomic,retain)UITextField *inputTextField;
@end
2、初始化及实现方法 :
#import "LTView.h"
@interface LTView()<UITextFieldDelegate>
{
CGRect _frame;
}
@end
@implementation LTView
-(void)dealloc{
[_titleLabel release];
[_inputTextField release];
[super dealloc];
}
-(id)initWithFrame:(CGRect)frame{
frame=CGRectMake(0, frame.origin.y, CGRectGetWidth([[UIScreen mainScreen]bounds]), 40);
self=[super initWithFrame:frame];
if (self) {
}
return self;
}
-(UILabel *)titleLabel{
if (!_titleLabel) {
CGFloat width=(CGRectGetWidth(self.frame)-85)/7;
self.titleLabel=[[[UILabel alloc]initWithFrame:CGRectMake(40, 5, width*2, 30)]autorelease];
_titleLabel.textAlignment=NSTextAlignmentLeft;
[self addSubview:_titleLabel];
}
return _titleLabel;
}
-(UITextField *)inputTextField{
if (!_inputTextField) {
CGFloat width=(CGRectGetWidth(self.frame)-85)/7;
self.inputTextField=[[[UITextField alloc]initWithFrame:CGRectMake(self.titleLabel.frame.origin.x+CGRectGetWidth(self.titleLabel.frame)+5, 5, width*5, 30)]autorelease];
_inputTextField.borderStyle=UITextBorderStyleRoundedRect;
_inputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
_inputTextField.delegate=self;
[self addSubview:_inputTextField];
}
return _inputTextField;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"%s",__FUNCTION__);
[textField resignFirstResponder];
return YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
第三步:实现LoginViewController
#import "LoginViewController.h"
#import "LTView.h"
@interface LoginViewController ()<UITextFieldDelegate,UIAlertViewDelegate>
@end
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor whiteColor];
self.view.tag=0001;
NSArray *arrays=@[@[@"用户名",@""],@[@"密码",@""]];
for (int i=0; i<arrays.count; i++) {
LTView *aView=[[[LTView alloc]initWithFrame:CGRectMake(0, 100+50*i, 0, 0)]autorelease];
aView.tag=100+i;
aView.backgroundColor=[UIColor whiteColor];
NSArray *array=arrays[i];
aView.titleLabel.text=array[0];
aView.inputTextField.placeholder=array[1];
aView.inputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
aView.inputTextField.delegate=self;
[self.view addSubview:aView];
}
LTView *aView=(LTView *)[self.view viewWithTag:101];
aView.inputTextField.secureTextEntry=YES;
NSArray *title=@[@"登录",@"找回密码",@"注册"];
for (int i=0; i<title.count; i++) {
UIButton *abutton=[UIButton buttonWithType:UIButtonTypeSystem];
abutton.frame=CGRectMake(40+(75+35)*i, 100+50*2, 75, 30);
abutton.tag=1000+i;
abutton.backgroundColor=[UIColor whiteColor];
[abutton setTitle:title[i] forState:UIControlStateNormal];
[abutton addTarget:self action:@selector(HandleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:abutton];
}
}
-(void)HandleButtonAction:(UIButton *)sender{
switch (sender.tag) {
case 1000:{
LTView *aView1=(LTView *)[self.view viewWithTag:100];
LTView *aView2=(LTView *)[self.view viewWithTag:101];
if (([aView1.inputTextField.text isEqualToString:@"15389022933"])&&[aView2.inputTextField.text isEqualToString:@"1234567890"]) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"欢迎回来" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
alert.delegate=self;
[alert release];
}else{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"用户名或者密码错误,请核对后再试" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
[alert release];
}
break;
}case 1001:{
UIView *passView=[self.view.superview viewWithTag:0003];
[self.view.superview bringSubviewToFront:passView];
break;
}case 1002:{
UIView *registView=[self.view.superview viewWithTag:0002];
[self.view.superview bringSubviewToFront:registView];
}
default:
break;
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0 && [alertView.message isEqualToString:@"欢迎回来"]) {
UIView *mainView = [[UIView alloc] initWithFrame:self.view.frame];
mainView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:mainView];
[mainView release];
[self.view bringSubviewToFront:mainView];
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(self.view.bounds), 50)];
aLabel.text = @"欢迎回来,这里是登陆之后的主界面";
aLabel.adjustsFontSizeToFitWidth = YES;
aLabel.textColor = [UIColor redColor];
aLabel.textAlignment = NSTextAlignmentCenter;
[mainView addSubview:aLabel];
[aLabel release];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"%s",__FUNCTION__);
[textField resignFirstResponder];
return YES;
}
第四步:实现RegistViewController
#import "RegistViewController.h"
#import "LTView.h"
@interface RegistViewController ()<UITextFieldDelegate,UIAlertViewDelegate>
@end
@implementation RegistViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self.view.superview viewWithTag:<#(NSInteger)#>]
self.view.backgroundColor=[UIColor whiteColor];
self.view.tag=0002;
UIView *containerView=[[[UIView alloc]initWithFrame:self.view.bounds]autorelease];
containerView.backgroundColor=[UIColor whiteColor];
containerView.tag=1999;
[self.view addSubview:containerView];
NSArray *arrays=@[@[@"用户名",@"请输入用户名"],@[@"密码",@"请输入密码"],@[@"确认密码",@"再次输入密码"],@[@"手机号",@"请输入联系方式"],@[@"邮箱",@"请输入邮箱"]];
for (int i=0; i<arrays.count; i++) {
LTView *aView=[[[LTView alloc]initWithFrame:CGRectMake(0, 100+50*i, 0, 0)]autorelease];
aView.inputTextField.delegate=self;
aView.tag=200+i;
aView.backgroundColor=[UIColor whiteColor];
NSArray *array=arrays[i];
aView.titleLabel.text=array[0];
aView.inputTextField.placeholder=array[1];
[containerView addSubview:aView];
}
LTView *aView1=(LTView *)[self.view viewWithTag:201];
aView1.inputTextField.secureTextEntry=YES;
LTView *aView2=(LTView *)[self.view viewWithTag:202];
aView2.inputTextField.secureTextEntry=YES;
LTView *aView3=(LTView *)[self.view viewWithTag:203];
aView3.inputTextField.keyboardType=UIKeyboardTypeNumberPad;
NSArray *title=@[@"注册",@"取消"];
for (int i=0; i<title.count; i++) {
UIButton *abutton=[UIButton buttonWithType:UIButtonTypeSystem];
abutton.frame=CGRectMake(90+(75+45)*i, 100+50*5+50, 75, 30);
abutton.tag=2000+i;
abutton.backgroundColor=[UIColor whiteColor];
[abutton setTitle:title[i] forState:UIControlStateNormal];
[abutton addTarget:self action:@selector(HandleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:abutton];
}
}
-(void)HandleButtonAction:(UIButton *)sender{
switch (sender.tag) {
case 2000:{
LTView *aView1=(LTView *)[self.view viewWithTag:200];
LTView *aView2=(LTView *)[self.view viewWithTag:201];
LTView *aView3=(LTView *)[self.view viewWithTag:202];
if (([aView1.inputTextField.text isEqualToString:@""])||[aView2.inputTextField.text isEqualToString:@""]||[aView3.inputTextField.text isEqualToString:@""]) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"用户名或者密码错误,请核对后再试" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
alert.delegate=self;
[alert release];
}else if(([aView1.inputTextField.text length]>6)&&[aView2.inputTextField.text isEqualToString:aView3.inputTextField.text]){
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"注册成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}
break;
}case 2001:{
UIView *loginView=[self.view.superview viewWithTag:0001];
[self.view.superview bringSubviewToFront:loginView];
break;
}
default:
break;
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if ((buttonIndex==0)&&[alertView.message isEqualToString:@"注册成功"]) {
UIView *loginView=[self.view.superview viewWithTag:0001];
[self.view.superview bringSubviewToFront:loginView];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"%s",__FUNCTION__);
[textField resignFirstResponder];
return YES;
}
第五步:实现PasswordViewController
#import "PasswordViewController.h"
//#import "LTView.h"
@interface PasswordViewController ()<UITextFieldDelegate,UIAlertViewDelegate>
@end
@implementation PasswordViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor whiteColor];
self.view.tag=0003;
NSArray *arrays=@[@"电子邮箱"];
for (int i=0; i<arrays.count; i++) {
UITextField *aView=[[[UITextField alloc]initWithFrame:CGRectMake(50, 100+50*i, CGRectGetWidth(self.view.bounds)-100, 30)]autorelease];
aView.tag=300+i;
aView.delegate=self;
aView.backgroundColor=[UIColor whiteColor];
aView.placeholder=arrays[i];
aView.borderStyle=UITextBorderStyleRoundedRect;
[self.view addSubview:aView];
}
NSArray *title=@[@"找回",@"取消"];
for (int i=0; i<title.count; i++) {
UIButton *abutton=[UIButton buttonWithType:UIButtonTypeSystem];
abutton.frame=CGRectMake(90+(75+45)*i, 100+50*5+50, 75, 30);
abutton.tag=3000+i;
abutton.backgroundColor=[UIColor whiteColor];
[abutton setTitle:title[i] forState:UIControlStateNormal];
[abutton addTarget:self action:@selector(HandleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:abutton];
}
}
-(void)HandleButtonAction:(UIButton *)sender{
switch (sender.tag) {
case 3000:{
UITextField *aTextField=(UITextField *)[self.view viewWithTag:300];
if ([aTextField.text isEqualToString:@"283380371@qq.com"]) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"验证码已发送到邮箱,请查收!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
alert.delegate=self;
[alert release];
}
else {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"邮箱不正确,请重新输入" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
[alert release];
}
break;
}case 3001:{
UIView *loginView=[self.view.superview viewWithTag:0001];
[self.view.superview bringSubviewToFront:loginView];
break;
}
default:
break;
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if ((buttonIndex==0)&&[alertView.message isEqualToString:@"验证码已发送到邮箱,请查收!"]) {
UIView *loginView=[self.view.superview viewWithTag:0001];
[self.view.superview bringSubviewToFront:loginView];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"%s",__FUNCTION__);
[textField resignFirstResponder];
return YES;
}
第六步:在AppDelegate.m加载主控制器:
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()<UITextFieldDelegate>
@end
@implementation AppDelegate
-(void)dealloc{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[[MainViewController alloc] init] autorelease];
return YES;
}