#import "AppDelegate.h"app
@interface AppDelegate () <UITextFieldDelegate>ide
@end ui
@implementation AppDelegate this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {spa
// Override point for customization after application launch..net
_window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];3d
[_window setBackgroundColor:[UIColor whiteColor]];代理
//================================rest
//1.建立textField对象orm
UITextField *field = [[UITextField alloc] initWithFrame:
CGRectMake(100, 100, 200, 60)];
[field setBorderStyle:UITextBorderStyleRoundedRect];
[_window addSubview:field];
UITextField *field2 = [[UITextField alloc] initWithFrame:
CGRectMake(100, 200, 200, 60)];
[field2 setBorderStyle:UITextBorderStyleRoundedRect];
field2.tag = 77;
field.tag = 7;
[_window addSubview:field2];
//2.设置代理
//把当前对象做为field的代理,那么必须让self的类去遵照协议实现协议方法;
field.delegate = self;
field2.delegate = self;
//3.显示清除按钮
[field setClearButtonMode:UITextFieldViewModeWhileEditing];
[_window makeKeyAndVisible];
return YES;
}
#pragma mark -- UITextField Delegate
//当用户点击textField弹出系统键盘上返回键的时候调用这个方法
//返回值:是否返回
//参数:就是当前委托这里的委托就是field,也就是这个方法的参数textFiled
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//设置委托的背景颜色
textField.backgroundColor = [UIColor yellowColor];
//textField放弃第一响应者(收起键盘)
//键盘就是第一响应者
[textField resignFirstResponder];
return YES;
}
//当文本输入框已经开始编辑的时候调用这个方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.tag == 7) {
NSLog(@"第一个已经开始编辑");
}
else {
NSLog(@"第二个已经开始编辑");
}
return YES;
}
//当文本输入框已经中止编辑的时候调用这个方法
//两种中止编辑的状况:1.放弃第一响应者2.点击了其余输入框;
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField.tag == 7) {
NSLog(@"第一个已经中止编辑");
}
else {
NSLog(@"第二个已经中止编辑");
}
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
//返回值:是否经过点击键盘的按钮的值去改变文本的内容
//参数一:委托
//参数二:当前输入字符的位置
//参数三:当前输入的字符(字符串形式返回)
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string{
NSLog(@"%@",NSStringFromRange(range));
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
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