UIdemo - UI学习第三天 3个页面 练习

//
//  AppDelegate.m
//  UIWork-day02
//
//  Created by lanou3g on 15/10/23.
//  Copyright (c) 2015年 Object. All rights reserved.
//

#import "AppDelegate.h"

#define k_margin 20
#define k_width 100
#define k_height 45

//定义枚举值--用来区分各种输入框
typedef enum : NSUInteger {
    
    loginView_userNameTag = 101, //登录界面下用户名框
    registerView_userNameTag,   //注册界面下用户名框
    loginView_userPwdTag,        //登录界面下密码框
    registerView_userPwdTag,    //注册界面下密码框
    confirmPwdTag,     //确认密码框
    phoneNumTag,       //手机号框
    mailTag,           //注册界面邮箱框
    findPwd_mailTag,   //找回密码界面邮箱框
    
    
} TagType;



@interface AppDelegate ()<UITextFieldDelegate>

@property (nonatomic,strong)UIView *loginView;     //登录界面
@property (nonatomic,strong)UIView *registerView;  //注册界面
@property (nonatomic,strong)UIView *findPwdView;   //找回密码界面


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor orangeColor];
    [self.window makeKeyAndVisible];
    
    //注册页面
    self.registerView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.registerView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:self.registerView];
    
    // 找回密码页面
    self.findPwdView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.findPwdView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:self.findPwdView];
    
    //登录页面
    self.loginView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.loginView.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:self.loginView];

    
    
    
    
    // 调用布局注册页面的方法
    [self setUpRegisterViewSubView];
    //调用找回密码页面的方法
    [self setUpFindPwdViewSubView];
    //调用布局登录页面的方法
    [self setUpLoginViewSubView];
    
    
    
    
    
    
    
    return YES;
}

#pragma mark 布局 【登录页面】的方法
-(void)setUpLoginViewSubView{
    //用户名 UILabel
    UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(k_margin, k_height, k_width, k_height)];
    userNameLabel.text = @"用户名:";
    [self.loginView addSubview:userNameLabel];
    
    
    
    //用户名 UITextField
    UITextField *userNameTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(userNameLabel.frame), CGRectGetMinY(userNameLabel.frame), k_width * 2, k_height)];
    userNameTF.placeholder = @"请输入用户名";
    userNameTF.borderStyle = UITextBorderStyleRoundedRect;
    userNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    userNameTF.keyboardType = UIKeyboardTypeDefault;
    [self.loginView addSubview:userNameTF];
    
    //把  return 键 设置为 next
    userNameTF.returnKeyType = UIReturnKeyNext;
    //设置代理
    userNameTF.delegate = self;
    //设置 userNameTF 的 Tag 值
    userNameTF.tag = loginView_userNameTag;
    
    
    
    
    
    
    // 密码 Label
    UILabel *userPwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(userNameLabel.frame), CGRectGetMaxY(userNameLabel.frame) + k_margin, k_width, k_height)];
    userPwdLabel.text = @"密    码:";
    [self.loginView addSubview:userPwdLabel];
    
    //密码 userPwdTF
    UITextField *userPwdTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(userNameTF.frame), CGRectGetMaxY(userNameTF.frame) + k_margin, k_width * 2, k_height)];
    userPwdTF.placeholder = @"请输入密码";
    userPwdTF.borderStyle = UITextBorderStyleRoundedRect;
    userPwdTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    userPwdTF.secureTextEntry = YES;
    userPwdTF.keyboardType = UIKeyboardTypeDefault;
    [self.loginView addSubview:userPwdTF];
    
    userPwdTF.delegate = self;
    userPwdTF.tag = loginView_userPwdTag;

    
    
    // 登录按钮
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(CGRectGetMinX(userPwdLabel.frame) + k_margin, CGRectGetMaxY(userPwdLabel.frame) + k_margin, k_width - 30, k_height - 10);
    
    //给 loginButton 设置背景图片
    UIImage *loginImage = [UIImage imageNamed:@"button.png"];
    [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
    // 添加 “登录” 两字
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginView addSubview:loginButton];
    // 绑定点击事件
    [loginButton addTarget:self action:@selector(loginButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    

    
    //--------------------------------------------------
    // 找回密码  按钮
    UIButton *findPwdButton = [UIButton buttonWithType:UIButtonTypeCustom];
    findPwdButton.frame = CGRectMake(CGRectGetMaxX(loginButton.frame) + k_margin, CGRectGetMinY(loginButton.frame), k_width, k_height - 10);
    // 给 findPwdButton 设置背景图片
    UIImage *findPwdImage = [UIImage imageNamed:@"button.png"];
    [findPwdButton setBackgroundImage:findPwdImage forState:UIControlStateNormal];
    //添加 “找回密码”
    [findPwdButton setTitle:@"找回密码" forState:UIControlStateNormal];
    [self.loginView addSubview:findPwdButton];
    // 给“找回密码”按钮绑定事件
    [findPwdButton addTarget:self action:@selector(findPwdButtonDicClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    //----------------------------------------------
    //注册按钮
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(CGRectGetMaxX(findPwdButton.frame) + k_margin, CGRectGetMinY(findPwdButton.frame), k_width - 30, k_height - 10);
    UIImage *registerImage = [UIImage imageNamed:@"button.png"];
    [registerButton setBackgroundImage:registerImage forState:UIControlStateNormal];
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.loginView addSubview:registerButton];
    // 给注册按钮 绑定事件
    [registerButton addTarget:self action:@selector(registerButtonDicClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}

#pragma mark 【登录】按钮的点击事件
-(void)loginButtonDidClicked:(UIButton *)sender{
    

    
    UIAlertView *alterRight = [[UIAlertView alloc]initWithTitle:@"提示" message:@"欢迎回来" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    [alterRight show];
    
    
}
#pragma mark 【注册】按钮的点击事件
-(void)registerButtonDicClicked:(UIButton *)sender{
    
    self.loginView.hidden = YES;
    self.findPwdView.hidden = YES;
    self.registerView.hidden = NO;
    
}
#pragma mark 【找回密码】按钮的点击事件
-(void)findPwdButtonDicClicked:(UIButton *)sender{
    self.loginView.hidden = YES;
    self.findPwdView.hidden = NO;
    self.registerView.hidden = YES;
}







#pragma mark 布局 【注册页面】的方法
-(void)setUpRegisterViewSubView{
    
    //用户名 userNameLabel
    UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(k_margin, k_height, k_width, k_height)];
    userNameLabel.text = @"用户名:";
    [self.registerView addSubview:userNameLabel];
    
    //用户名 userNameTF
    UITextField *userNameTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(userNameLabel.frame), CGRectGetMinY(userNameLabel.frame), k_width * 2, k_height)];
    userNameTF.placeholder = @"请输入用户名";
    userNameTF.borderStyle = UITextBorderStyleRoundedRect;
    userNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    userNameTF.keyboardType = UIKeyboardTypeDefault;
    [self.registerView addSubview:userNameTF];
    
    userNameTF.returnKeyType = UIReturnKeyNext;
    userNameTF.delegate = self;
    userNameTF.tag = registerView_userNameTag;
    
    
    // 密码 userPwdLabel
    UILabel *userPwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(userNameLabel.frame), CGRectGetMaxY(userNameLabel.frame) + k_margin, k_width, k_height)];
    userPwdLabel.text = @"密    码:";
    [self.registerView addSubview:userPwdLabel];
    
    //密码 userPwdTF
    UITextField *userPwdTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(userNameTF.frame), CGRectGetMaxY(userNameTF.frame) + k_margin, k_width * 2, k_height)];
    userPwdTF.placeholder = @"请输入密码";
    userPwdTF.borderStyle = UITextBorderStyleRoundedRect;
    userPwdTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    userPwdTF.secureTextEntry = YES;
    userPwdTF.keyboardType = UIKeyboardTypeDefault;
    [self.registerView addSubview:userPwdTF];
    
    userPwdTF.returnKeyType = UIReturnKeyNext;
    userPwdTF.delegate = self;
    userPwdTF.tag = registerView_userPwdTag;
    
    
    
    
    //确认密码  confirmPwdLabel
    UILabel *confirmPwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(userPwdLabel.frame), CGRectGetMaxY(userPwdLabel.frame) + k_margin, k_width, k_height)];
    confirmPwdLabel.text = @"确认密码:";
    [self.registerView addSubview:confirmPwdLabel];
    
    //确认密码  confirmPwdTF
    UITextField *confirmPwdTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(userPwdTF.frame), CGRectGetMaxY(userPwdTF.frame) + k_margin, k_width * 2, k_height)];
    confirmPwdTF.placeholder = @"再次输入密码";
    confirmPwdTF.borderStyle = UITextBorderStyleRoundedRect;
    confirmPwdTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    confirmPwdTF.secureTextEntry = YES;
    confirmPwdTF.keyboardType = UIKeyboardTypeDefault;
    [self.registerView addSubview:confirmPwdTF];
    
    confirmPwdTF.returnKeyType = UIReturnKeyNext;
    confirmPwdTF.delegate = self;
    confirmPwdTF.tag = confirmPwdTag;
    

    
    //手机号  phoneNumLabel
    UILabel *phoneNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(confirmPwdLabel.frame), CGRectGetMaxY(confirmPwdLabel.frame) + k_margin, k_width, k_height)];
    phoneNumLabel.text = @"手机号:";
    [self.registerView addSubview:phoneNumLabel];
    
    //手机号  phoneNumTF
    UITextField *phoneNumTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(confirmPwdTF.frame), CGRectGetMaxY(confirmPwdTF.frame) + k_margin,k_width * 2, k_height)];
    phoneNumTF.placeholder = @"请输入联系方式";
    phoneNumTF.borderStyle = UITextBorderStyleRoundedRect;
    phoneNumTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    phoneNumTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [self.registerView addSubview:phoneNumTF];
    
    phoneNumTF.returnKeyType = UIReturnKeyNext;
    phoneNumTF.delegate = self;
    phoneNumTF.tag = phoneNumTag;
    
    
    //邮箱   mailLabel
    UILabel *mailLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(phoneNumLabel.frame), CGRectGetMaxY(phoneNumLabel.frame) + k_margin, k_width, k_height)];
    mailLabel.text = @"邮箱:";
    [self.registerView addSubview:mailLabel];
    
    //邮箱 mailTF
    UITextField *mailTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(phoneNumTF.frame), CGRectGetMaxY(phoneNumTF.frame) + k_margin,k_width * 2, k_height)];
    mailTF.placeholder = @"请输入邮箱";
    mailTF.borderStyle = UITextBorderStyleRoundedRect;
    mailTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    mailTF.keyboardType = UIKeyboardTypeEmailAddress;
    [self.registerView addSubview:mailTF];
    
    mailTF.delegate = self;
    mailTF.tag = mailTag;

    
    // 注册按钮
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    registerButton.frame = CGRectMake(CGRectGetMidX(mailLabel.frame), CGRectGetMaxY(mailLabel.frame) + k_margin, k_width, k_height);
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.registerView addSubview:registerButton];
    
    
    // 取消按钮
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(CGRectGetMaxX(registerButton.frame) + k_margin, CGRectGetMaxY(mailLabel.frame) + k_margin, k_width, k_height);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.registerView addSubview:cancelButton];
    //给 取消 按钮绑定事件
    [cancelButton addTarget:self action:@selector(registerView_cancelButtonDicClicked:) forControlEvents:UIControlEventTouchUpInside];

    
    
    
    
}

#pragma mark 布局 【找回密码页面】的方法
-(void)setUpFindPwdViewSubView{
    
    //  "电子邮箱" findPwdTF
    UITextField *findPwdTF = [[UITextField alloc]initWithFrame:CGRectMake(k_margin * 2, k_margin * 4, k_width * 3, k_height)];
    findPwdTF.placeholder = @"电子邮箱";
    findPwdTF.borderStyle = UITextBorderStyleRoundedRect;
    findPwdTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    findPwdTF.keyboardType = UIKeyboardTypeEmailAddress;
    [self.findPwdView addSubview:findPwdTF];
    
    findPwdTF.delegate = self;
    findPwdTF.tag = findPwd_mailTag;
    
    // 找回 按钮
    UIButton *findButton = [UIButton buttonWithType:UIButtonTypeSystem];
    findButton.frame = CGRectMake(k_margin * 3, k_margin * 7, k_width, k_height);
    [findButton setTitle:@"找回" forState:UIControlStateNormal];
    [self.findPwdView addSubview:findButton];
    
    
    
    // 取消 按钮
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(k_margin * 10, k_margin * 7, k_width, k_height);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.findPwdView addSubview:cancelButton];
    //给取消按钮绑定事件
    [cancelButton addTarget:self action:@selector(findPwdView_cancelButtonDicClicked:) forControlEvents:UIControlEventTouchUpInside];
    
 
}
#pragma mark 【找回密码】页面下【取消】按钮的点击事件
-(void)findPwdView_cancelButtonDicClicked:(UIButton *)sender{
    
    self.loginView.hidden = NO;
    self.findPwdView.hidden = YES;
    self.registerView.hidden = YES;
    
}
#pragma mark  【注册页面】下【取消】按钮的点击事件
-(void)registerView_cancelButtonDicClicked:(UIButton *)sender{
    self.loginView.hidden = NO;
    self.findPwdView.hidden = YES;
    self.registerView.hidden = YES;

}



#pragma mark UITextField 里面的代理方法
// 此方法主要用来回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    //根据 tag 值拿出对应的 textField
    // loginView  页面下有2个输入框
    UITextField *login_usernameTF = (UITextField *)[self.loginView viewWithTag:loginView_userNameTag];
    UITextField *login_userpwdTF = (UITextField *)[self.loginView viewWithTag:loginView_userPwdTag];
    //  registerView  页面下有5个输入框
    UITextField *register_usernameTF = (UITextField *)[self.registerView viewWithTag:registerView_userNameTag];
    UITextField *register_userpwdTF = (UITextField *)[self.registerView viewWithTag:registerView_userPwdTag];
    UITextField *register_confirmPwdTF = (UITextField *)[self.registerView viewWithTag:confirmPwdTag];
    UITextField *register_phoneNumTF = (UITextField *)[self.registerView viewWithTag:phoneNumTag];
    UITextField *register_mailTF = (UITextField *)[self.registerView viewWithTag:mailTag];
    //   findPwdView 界面下有1个输入框
    UITextField *findPwdView_mailTF = (UITextField *)[self.findPwdView viewWithTag:findPwd_mailTag];
    
    
    
    // 根据 tag 值 判断,分别做不同的响应
    // 在 【loginView】页面下
    if (textField.tag == loginView_userNameTag) {
        //撤销用户名输入框的第一响应者身份
        [login_usernameTF resignFirstResponder];
        //让密码输入框变成第一响应者
        [login_userpwdTF becomeFirstResponder];
  
    } else {
        [textField resignFirstResponder];
    }
    
    // 在【registerView】页面下
    if (textField.tag == registerView_userNameTag) {
        [register_usernameTF resignFirstResponder];
        [register_userpwdTF becomeFirstResponder];
    } else if (textField.tag == registerView_userPwdTag){
        [register_userpwdTF resignFirstResponder];
        [register_confirmPwdTF becomeFirstResponder];
    } else if (textField.tag == confirmPwdTag){
        [register_confirmPwdTF resignFirstResponder];
        [register_phoneNumTF becomeFirstResponder];
    } else if (textField.tag == phoneNumTag){
        [register_phoneNumTF resignFirstResponder];
        [register_mailTF becomeFirstResponder];
    } else {
        [textField resignFirstResponder];
    }
    
    // 在【findPwdView】界面下
    if (textField.tag == findPwd_mailTag) {
        [findPwdView_mailTF resignFirstResponder];
    } else {
        [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

你可能感兴趣的:(UIdemo - UI学习第三天 3个页面 练习)