iOS编程-------UITextField UIButton UITextFieldDelegate

//
//  AppDelegate.h
//  UI02_UITextField\UIButton\UITextFieldDelegate
//
//  Created by t on 15/9/1.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITextFieldDelegate>

@property (strong, nonatomic) UIWindow *window;

@end


//
//  AppDelegate.m
//  UI02_UITextField\UIButton\UITextFieldDelegate
//
//  Created by t on 15/9/1.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()
#pragma 把我们要使用的控件定义为实例变量.
@property (nonatomic, retain)UITextField *userNameTF;
@property (nonatomic, retain)UITextField *keyTF;
@end

@implementation AppDelegate
#pragma 重写dealloc方法
- (void)dealloc{
    [_keyTF release];
    [_userNameTF release];
    [_window release];
    [super dealloc];
}

#pragma 实现UITextFieldDelegate里面的键盘回收方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    //取消第一响应者
    //正在和用户进行交互的称为响应者,同一时间内只能有一个响应者.

    //点击return按钮 让当前输入框取消第一响应者,让对应的另外一个输入框成为第一响应者.
//    if (textField.tag == 101) {
//        [textField resignFirstResponder];
//        //取出来 密码输入框
//        UITextField *pwdTextField = (UITextField *)[self.window viewWithTag:102];  //注意返回值类型
//        //让密码输入框 成为2第一个响应者
//        [pwdTextField becomeFirstResponder];
//}

//        if ([textField isEqual:_userNameTF]) {
//            [textField resignFirstResponder];
//            UITextField *nextTextField = [self.window ];
// }

    return YES;
}


- (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];

#pragma UI02 主讲内容:UITextField,UIButton,UITextFieldDelegate
#pragma 一.UITextField
// 创建文本框
//    1.开辟空间,并且初始化
//    UITextField *userNameTF = [[UITextField alloc] initWithFrame:(CGRectMake(100, 50, 200, 30))];

//    2.设置相关属性,外观属性,输入属性,显示属性
//    (1)显示属性
//    userNameTF.text = @"用户名";
//    userNameTF.textColor = [UIColor blueColor];
//    userNameTF.font = [UIFont systemFontOfSize:15];

//    userNameTF.placeholder = @"请输入用户名";//设置占位符
//    userNameTF.textColor = [UIColor redColor];
    (2)输入属性
//    userNameTF.secureTextEntry = YES;//设置为密文输入,默认为NO
//    userNameTF.keyboardType = UIKeyboardTypeNumberPad;//设置键盘样式:数字键盘
    (3)外观属性
//    userNameTF.borderStyle = UITextBorderStyleRoundedRect;//设置边框样式,圆角样式
//    userNameTF.clearButtonMode = UITextFieldViewModeAlways;//清除样式,永久显示

    3.添加到父视图上
//    [self.window addSubview:userNameTF];
//
//    4.释放
//    [userNameTF release];

#pragma 练习 (1).输入用户名
    UITextField *userNameTF = [[UITextField alloc] initWithFrame:(CGRectMake(50, 50, 200, 30))];
    userNameTF.placeholder = @"请输入用户名";
    userNameTF.textColor = [UIColor blueColor];
    userNameTF.borderStyle = UITextBorderStyleRoundedRect;
    userNameTF.clearButtonMode = UITextFieldViewModeAlways;
//    [self.window addSubview:userNameTF];
//    [userNameTF release];

    userNameTF.delegate = self;
    userNameTF.tag = 101;
    [self.window addSubview:userNameTF];
    [userNameTF release];

#pragma (2).输入密码
    UITextField *keyTF = [[UITextField alloc] initWithFrame:(CGRectMake(50, 90, 200, 30))];
    keyTF.placeholder = @"请输入密码";
    keyTF.textColor = [UIColor blackColor];
    keyTF.secureTextEntry = YES;
    keyTF.keyboardType = UIKeyboardTypeNumberPad;
    keyTF.borderStyle = UITextBorderStyleRoundedRect;
    keyTF.clearButtonMode = UITextFieldViewModeAlways;
//    [self.window addSubview:keyTF];
//    [keyTF release];

    keyTF.delegate = self;
    keyTF.tag = 102;
    [self.window addSubview:keyTF];
    [keyTF release];

#pragma 二. UIButton 按钮
    /*UIButton 是UIControl 的子类, 主要功能是和用户进行交互,响应用户的点击事件.
    UIButton 一般使用类方法创建,使用系统提供的按钮样式,因此不用release */
    //登陆
    //1.创建Button
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    //设置Button系统样式

    //2.设置Button属性

    loginButton.frame = CGRectMake(40, 210, 30, 20);
//    [loginButton setTitle:@"登陆" forState:(UIControlStateNormal)];//设置Button的标题
    [loginButton setTitle:@"xxx" forState:UIControlStateHighlighted];


    //3.添加点击事件
    //addTarget : 执行selector 方法的对象
    //self 在哪个类的内部,self就是那个类的一个实例化对象
    //action: 点击事件触发的方法
    //forControlEvents: 触发事件
    [loginButton addTarget:self action:@selector(loginAction:)  forControlEvents:(UIControlEventTouchUpInside)];

//  添加背景图片  通过UIImage 类去创建一个图片对象
//    如果图片为png格式,不用加后缀,否则需要加后缀
    UIImage *image = [UIImage imageNamed:@"EG$Z4XLKDA01CBH5R1`@]}4"];
//    设置normal状态下的背景图
    [loginButton setBackgroundImage:image forState:UIControlStateNormal];

    //4.添加button 到父视图上
    [self.window addSubview:loginButton];

#pragma 找回密码
    UIButton *getKeyButton = [UIButton buttonWithType:UIButtonTypeSystem];
    getKeyButton.frame = CGRectMake(100, 200, 80, 44);
    [getKeyButton setTitle:@"找回密码" forState:UIControlStateNormal];
    [getKeyButton addTarget:self action:@selector(getKeyAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:getKeyButton];

#pragma 注册
    UIButton *registrationButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    registrationButton.frame = CGRectMake(180, 210, 30, 20);
    [registrationButton setTitle:@"注册" forState:UIControlStateNormal];
    [registrationButton addTarget:self action:@selector(registrationAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:registrationButton];
    //添加背景图片
    UIImage *image2 = [UIImage imageNamed:@"/Users/lanou/Desktop/9ZDCZFDD5H3%5NX36(`][EU.jpg"];
    [registrationButton setBackgroundImage:image2 forState:UIControlStateNormal];

#pragma 三. UITextFieldDelegate协议
    //键盘回收
    //键盘回收 ,主要通过textFieldDelegate 协议里面的方法textFieldShouldReturn: 来实现
    //当我们点击虚拟键盘return按钮的时候,textField的代理区执行textFieldShouldReturn:方法实现键盘回收,或者其他操作
    //第一步:delegate协议  //UItextFieldDelegate
    //第二步:delegate 实现协议里面的方法  - (BOOL)textFieldShouldReturn:(UITextField *)textField;

    UITextField *textField2 = [[UITextField alloc] initWithFrame:(CGRectMake(100, 300, 200, 30))];
    textField2.placeholder = @"回收键盘";
    textField2.borderStyle = UITextBorderStyleRoundedRect;
    //第三步 给textField 设置代理
    textField2.delegate = self;
    [self.window addSubview:textField2];
    [textField2 release];


    return YES;

}

//点击事件触发的方法,登陆事件
- (void)loginAction:(UIButton *)button{
//    button.hidden = YES;  //隐藏
    [button removeTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"进行安全判定, 然后进入登陆页面");
}

- (void)getKeyAction:(UIButton *)button{
    [button removeTarget:self action:@selector(getKeyAction:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"找回密码");
}

- (void)registrationAction:(UIButton *)button{
    [button removeTarget:self action:@selector(registrationAction:) forControlEvents:UIControlEventTouchUpOutside];
    NSLog(@"注册");
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值