#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