登陆页面,找回密码,注册页面(三个view的切换)

//

//  AppDelegate.m

//  UI_Lesson2_homework

//

//  Created by 李洪鹏 on 15/7/1.

//  Copyright (c) 2015年 李洪鹏. All rights reserved.

//

#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic, copy)NSString *userName;   //用户名

@property (nonatomic, copy)NSString *passWord;   // 密码

@property (nonatomic, retain)UIView *loginView; //登陆界面

@property (nonatomic, retain)UIView *findpassWordView;  // 找回密码界面

@property (nonatomic, retain)UIView *registView;  //注册界面

@end

@implementation AppDelegate

-(void)dealloc

{

    [_userName release];

    [_passWord release];

    [_loginView release];

    [_findpassWordView release];

    [_registView release];

    [super dealloc];

     

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    

    self.window = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    self.userName = @"root";

    self.passWord = @"123456";

    

#pragma mark -------登陆

    self.loginView = [[[UIView alloc]initWithFrame:self.window.bounds] autorelease];

    self.loginView.backgroundColor = [UIColor magentaColor];

    [self.window addSubview:_loginView];

    

    //用户

    UILabel *nameLabel = [[[UILabel alloc]initWithFrame:CGRectMake(30, 60, 80, 45)] autorelease];

    nameLabel.text = @"用户名";

    [self.loginView addSubview:nameLabel];

    

    UITextField *nameField = [[[UITextField alloc] initWithFrame:CGRectMake(110, 60, 200, 45)] autorelease];

    nameField.placeholder = @"请输入用户名";

    nameField.tag = 200;

    nameField.borderStyle = UITextBorderStyleRoundedRect;

    [self.loginView addSubview:nameField];

    

    //设置代理

    nameField.delegate = self;

    

#pragma mark -------密码

    UILabel *passWordLabel = [[[UILabel alloc] initWithFrame:CGRectMake(30, 120, 80, 40)] autorelease];

    passWordLabel.text = @"密码";

    [self.loginView addSubview:passWordLabel];

    

    UITextField *passWordField = [[[UITextField alloc] initWithFrame:CGRectMake(110, 120, 200, 40)] autorelease];

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

    passWordField.secureTextEntry = YES;

    passWordField.borderStyle = UITextBorderStyleRoundedRect;

    passWordField.tag = 201;

    [self.loginView addSubview:passWordField];

    

    //设置代理

    passWordField.delegate = self;

    

    //循环创建button

    NSArray *titles = @[@"登录", @"找回密码", @"注册"];   //语法糖

    

    for (int i  = 0; i < 3; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.frame = CGRectMake(20 + (20 + 105) * i, 200, 105, 40);

        //动态赋值title

        [button setTitle:titles[i] forState:UIControlStateNormal];

       

        button.tag = 100 + i;

        [_loginView addSubview:button];

    }

    //获取button

    UIButton *button1 = (UIButton *)[self.loginView viewWithTag:100];

    UIButton *button2 = (UIButton *)[self.loginView viewWithTag:101];

    UIButton *button3 = (UIButton *)[self.loginView viewWithTag:102];

    

    //添加button的事件

    [button1 addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];

    [button2 addTarget:self action:@selector(passwordAction:) forControlEvents:UIControlEventTouchUpInside];

    [button3 addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];

    

#pragma mark  ----------- 找回密码---------

    //创建找回密码界面

    self.findpassWordView = [[[UIView alloc] initWithFrame:self.window.bounds] autorelease];

    self.findpassWordView.backgroundColor = [UIColor blueColor];

    [self.window addSubview:self.findpassWordView];

    

    //输入框

    UITextField *emailField = [[[UITextField alloc] initWithFrame: CGRectMake(50, 50, 275, 40)] autorelease];

    emailField.placeholder = @"请输入正确的邮箱";

    emailField.borderStyle = UITextBorderStyleRoundedRect;

    [self.findpassWordView addSubview:emailField];

    

    //找回button

    UIButton *findButton = [UIButton buttonWithType:UIButtonTypeSystem];

    findButton.frame = CGRectMake(100, 150, 70, 40);

    [findButton setTitle:@"找回" forState:UIControlStateNormal];

    [findButton addTarget:self action:@selector(findACtion:) forControlEvents:UIControlEventTouchUpInside];

    [_findpassWordView addSubview:findButton];

    

    //取消

    UIButton *canleButton = [UIButton buttonWithType:UIButtonTypeSystem];

    canleButton.frame = CGRectMake(205, 150, 70, 40);

    [canleButton setTitle:@"取消" forState:UIControlStateNormal];

    [canleButton addTarget:self action:@selector(canaleAction:) forControlEvents:(UIControlEventTouchUpInside)];

    [_findpassWordView addSubview:canleButton];

    

#pragma mark --------------------注册界面

    //创建注册界面

    self.registView = [[[UIView alloc] initWithFrame:self.window.bounds] autorelease];

    self.registView.backgroundColor = [UIColor yellowColor];

    [self.window addSubview:self.registView];

    

    //创建数组

    NSArray *labelText = @[@"用户名", @"密码", @"确认密码", @"手机号",@"邮箱"];

    NSArray *placeholders = @[@"请输入用户名", @"请输入密码", @"确认密码", @"请输入手机号",@"请输入邮箱"];

    

    //循环创建

    for (int i = 0; i < labelText.count; i++) {

        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(30, 60 + (40 + 20) * i, 80, 40)] autorelease];

        label.text = labelText[i];

        [_registView addSubview:label];

        

        UITextField *tf = [[[UITextField alloc] initWithFrame:CGRectMake(110, 60 + (40 + 20) * i, 200, 40)] autorelease];

        tf.placeholder = placeholders[i];

        tf.borderStyle = UITextBorderStyleRoundedRect;

        tf.tag = 300 + i;

        tf.delegate = self;

        //密文

        if ( i == 1 || i == 2) {

            tf.secureTextEntry = YES;

        }

        [_registView addSubview:tf];

        

        

    }

    

    UIButton *findButton2 = [UIButton buttonWithType:UIButtonTypeSystem];

    findButton2.frame = CGRectMake(100, 400, 100, 100);

    findButton2.backgroundColor = [UIColor redColor];

    [findButton2 setTitle:@"登录" forState:(UIControlStateNormal)];

    [findButton2 addTarget:self action:@selector(findButton2Action:) forControlEvents:(UIControlEventTouchUpInside)];

    

    [self.registView addSubview:findButton2];

    

    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];

    cancelButton.frame = CGRectMake(200, 400, 100, 100);

    cancelButton.backgroundColor =  [UIColor greenColor];

    [cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:(UIControlEventTouchUpInside)];

    [self.registView addSubview:cancelButton];

    

    //每次运行的时候插入登录界面

    [self.window bringSubviewToFront:self.loginView];

    

    return YES;

}

//取消按钮

- (void)cancelAction:(UIButton *)sender

{

    [self.window bringSubviewToFront:self.loginView];

}

- (void)findButton2Action:(UIButton *)sender

{

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil];

    //通过判定 显示不同的信息

    UITextField *nameTF = (UITextField *)[self.registView viewWithTag:300];

    UITextField *passWordTF = (UITextField *)[self.registView viewWithTag:301];

    UITextField *passWordTF2 = (UITextField *)[self.registView viewWithTag:302];

    

    if ((nameTF.text.length > 0 || passWordTF.text.length > 0 || passWordTF2.text.length == 0) && [passWordTF.text isEqualToString:passWordTF2.text]) {

        //记录更新,新的账号密码

        self.userName = nameTF.text;

        self.passWord = passWordTF.text;

        //注册没有问题

        alertView.message = [NSString stringWithFormat:@"注册成功! 新的用户名%@ 密码%@", self.userName, self.passWord];

        [alertView show];

        

    }else{

        

        alertView.message = @"注册失败";

        

        

    }

    [alertView show];

    [alertView release];

}

//找回事件

- (void)findACtion:(UIButton *)sender

{

    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"账号信息" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"", nil] autorelease];

    alert.message = [NSString stringWithFormat:@"账号%@ 密码%@", _userName, _passWord];

    [alert show];

    

}

//取消事件

- (void)canaleAction:(UIButton *)sender

{

    //讲注册界面 移动到最前面

    [self.window bringSubviewToFront:_loginView];

}

//登陆事件

- (void)loginAction:(UIButton *)sender

{

    UIAlertView *alertView = [[[UIAlertView alloc]initWithTitle:@"通知" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil] autorelease];

    

    //根据用户名密码 是否正确显示不同的信息

    //获取输入框

    UITextField *tf1 = (UITextField *)[self.loginView viewWithTag:200];

    UITextField *tf2 = (UITextField *)[self.loginView viewWithTag:201];

    

    

    if ([tf1.text isEqualToString:self.userName] && [tf2.text isEqualToString:self.passWord]) {

        alertView.message = @"欢迎回来!";

    }else

    {

        alertView.message = @"用户名或密码错误";

    }

    

    [alertView show];

    

}

//找回密码

- (void)passwordAction:(UIButton *)sender

{

    [self.window bringSubviewToFront:_findpassWordView];

}

//注册按钮

- (void)registAction: (UIButton *)sender

{

    

    [self.window bringSubviewToFront:self.registView];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    //取消第一响应者

    [textField resignFirstResponder];

    return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end

原文地址:https://www.cnblogs.com/lhp-1992/p/4614385.html