UI day02

//
//  AppDelegate.m
//  UI-lesson2-zonghe
//
//  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 {
    
    userNameTag = 101,
    userPwdTag ,

} TagType;






@interface AppDelegate ()<UITextFieldDelegate>

@property(nonatomic,strong)UIView *loginView;   // 登录
@property(nonatomic,strong)UIView *registerView; // 注册


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    NSLog(@"程序启用的时候会调用%s",__func__);
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //注册页面
    self.registerView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.registerView.backgroundColor = [UIColor orangeColor];
    [self.window addSubview:self.registerView];
    
    //登录页面
    self.loginView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.registerView.backgroundColor = [UIColor orangeColor];
    [self.window addSubview:self.loginView];

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

#pragma mark 布局登录页面的方法
-(void)setUpLoginViewSubView{
    
    UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(k_margin, k_height, k_width, k_height)];
    userNameLabel.text = @"用户名:";
    [self.loginView addSubview:userNameLabel];
    
    
    
    
    UITextField *userNameTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(userNameLabel.frame), CGRectGetMinY(userNameLabel.frame), k_width * 2, k_height)];
    userNameTF.placeholder = @"请输入用户名";
    userNameTF.borderStyle = UITextBorderStyleRoundedRect;
    [self.loginView addSubview:userNameTF];
    
    // 把 return 键设置为 next
    userNameTF.returnKeyType = UIReturnKeyNext;
    
    
    //设置代理
    userNameTF.delegate = self;
    
    //设置 userNameTF 的 tag值
    userNameTF.tag = userPwdTag;
    
    
    
    
    
    UILabel *userPwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(userNameLabel.frame), CGRectGetMaxY(userNameLabel.frame) + k_margin, k_width, k_height)];
    userPwdLabel.text = @"密    码:";
    [self.loginView addSubview:userPwdLabel];
    
    
    
    UITextField *userPwdTF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(userNameTF.frame), CGRectGetMinY(userPwdLabel.frame), CGRectGetWidth(userNameTF.frame), k_height)];
    userPwdTF.placeholder = @"请输入密码";
    userPwdTF.borderStyle = UITextBorderStyleRoundedRect;
    userPwdTF.secureTextEntry = YES;
    [self.loginView addSubview:userPwdTF];
    
    //设置  AppDelegate 作为代理
    userPwdTF.delegate = self;
    
    userPwdTF.tag = userPwdTag;
    
    
    
    
    //登录按钮
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake(CGRectGetMinX(userPwdLabel.frame) + k_margin, CGRectGetMaxY(userPwdLabel.frame) + k_margin, k_width, k_height);
    //如果 图片 格式 是png,可以省略后缀,其他的都不可以省略
//    UIImage *loginImage = [UIImage imageNamed:@"5.jpg"];
    // 给 loginButton 设置背景图片
//    [loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
    
    //设置前景图片
//    [loginButton setImage:loginImage forState:UIControlStateNormal];
    
    // button 虽然可以设置图片,但是 button 的主要作用不在于显示图片,有专门显示图片的控件---UIImageView
    
    // button 写 “登录”两字
    [loginButton setTitle:@"login" forState:UIControlStateNormal];
    [self.loginView addSubview:loginButton];
    
    //绑定点击事件
    [loginButton addTarget:self action:@selector(loginButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(CGRectGetMaxX(loginButton.frame) + k_margin, CGRectGetMinY(loginButton.frame), k_width, k_height);
    [registerButton setTitle:@"register" forState:UIControlStateNormal];
    [self.loginView addSubview:registerButton];
    // 给 registerButton 绑定点击事件
    [registerButton addTarget:self action:@selector(registerButtonDicClicked:) forControlEvents:UIControlEventTouchUpInside];
    

    
}

#pragma mark UITextField 里面的代理方法
// 此方法主要用来回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    //撤销第一响应者
    [textField resignFirstResponder];
    
    //1.根据 tag 值拿出对应的 textField
    UITextField *nameTF = (UITextField *)[self.loginView viewWithTag:userNameTag];
    UITextField *pwdTF = (UITextField *)[self.loginView viewWithTag:userPwdTag];
    

    //2.根据 tag 值判断,分别做不同的事
    if (textField.tag == userNameTag) {
        
        //撤销用户名输入框的第一响应者身份
        [nameTF resignFirstResponder];
        //让密码输入框变成第一响应者
        [pwdTF becomeFirstResponder];
        
        
    } else {
        [textField resignFirstResponder];
    }
    

    return YES;
}


#pragma mark 登录按钮的点击事件
-(void)loginButtonDidClicked:(UIButton *)sender{
    NSLog(@"%s",__func__);
}

#pragma mark 注册按钮的点击事件

-(void)registerButtonDicClicked:(UIButton *)sender{

    //第一种方式:
//    [self.window bringSubviewToFront:self.registerView];
    
    
    // 第二种
    self.loginView.hidden = YES;
    self.registerView.hidden = NO;
    
    
    
}

#pragma mark 布局注册页面的方法
-(void)setUpRegisterViewSubView{
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(20, 30, 100, 40);
    [backButton setTitle:@"back" forState:UIControlStateNormal];
    [self.registerView addSubview:backButton];
    
    //绑定事件
    [backButton addTarget:self action:@selector(backButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    
  
    
}
#pragma mark 注册页面返回按钮的点击事件
-(void)backButtonDidClicked:(UIButton *)sender{
    //把登录页面调整到最前面
//    [self.window bringSubviewToFront:self.loginView];
    
    
    self.loginView.hidden = NO;
    self.registerView.hidden = YES;
    
    
}







#pragma mark app将要取消活跃状态(将要进入后台)

- (void)applicationWillResignActive:(UIApplication *)application {
    
    NSLog(@"app将要取消活跃状态%s",__func__);
}


#pragma mark   app已经进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    NSLog(@"app已经进入后台%s",__func__);
    
}


#pragma mark  app即将进入活跃状态(前台)
- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    NSLog(@" app即将进入活跃状态(前台)%s",__func__);
}


#pragma mark  app已经进入活跃状态
- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    NSLog(@"app已经进入活跃状态%s",__func__);
}


#pragma mark  app程序即将退出
- (void)applicationWillTerminate:(UIApplication *)application {
    
    NSLog(@"app程序即将退出%s",__func__);
}


@end

你可能感兴趣的:(UI day02)