效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController<UITextFieldDelegate> 4 @property (strong, nonatomic) UITextField *txtFMessage; 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 [self layoutUI]; 13 } 14 15 - (void)didReceiveMemoryWarning { 16 [super didReceiveMemoryWarning]; 17 // Dispose of any resources that can be recreated. 18 } 19 20 - (void)layoutUI { 21 _txtFMessage = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 80)]; 22 _txtFMessage.center = self.view.center; 23 _txtFMessage.borderStyle = UITextBorderStyleRoundedRect; 24 _txtFMessage.text = @"通过NSLog确认delegate的设置"; 25 _txtFMessage.textColor = [UIColor colorWithRed:0.342 green:1.000 blue:0.659 alpha:1.000]; 26 _txtFMessage.textAlignment = NSTextAlignmentLeft; 27 _txtFMessage.contentHorizontalAlignment = UIControlContentVerticalAlignmentBottom; 28 29 //设置delegate 30 _txtFMessage.delegate = self; 31 _txtFMessage.clearButtonMode = UITextFieldViewModeAlways; 32 [self.view addSubview:_txtFMessage]; 33 34 UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)]; 35 [self.view addGestureRecognizer:gestureRecognizer]; 36 } 37 38 /** 39 * 隐藏键盘 40 * 41 * @param sender 事件触发者 42 */ 43 - (IBAction)hideKeyboard:(id)sender { 44 NSLog(@"Tap Gesture Recognizer,去掉某些控件作为第一响应器"); 45 [_txtFMessage resignFirstResponder]; 46 } 47 48 #pragma mark - TextField 49 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 50 NSLog(@"textFieldShouldBeginEditing: %@", textField.text); 51 return YES; 52 } 53 54 - (void)textFieldDidBeginEditing:(UITextField *)textField { 55 NSLog(@"textFieldDidBeginEditing: %@", textField.text); 56 } 57 58 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { 59 NSLog(@"textFieldShouldEndEditing: %@", textField.text); 60 return YES; 61 } 62 63 - (void)textFieldDidEndEditing:(UITextField *)textField { 64 NSLog(@"textFieldDidEndEditing: %@", textField.text); 65 } 66 67 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 68 NSLog(@"shouldChangeCharactersInRange: %@", string); 69 return YES; 70 } 71 72 - (BOOL)textFieldShouldClear:(UITextField *)textField { 73 NSLog(@"textFieldShouldClear: %@", textField.text); 74 return YES; 75 } 76 77 - (BOOL)textFieldShouldReturn:(UITextField *)textField { 78 NSLog(@"textFieldShouldReturn: %@", textField.text); 79 return YES; 80 } 81 82 @end
输出结果:
1 2015-04-14 00:53:40.611 FirstBook065[1147:82365] Tap Gesture Recognizer,去掉某些控件作为第一响应器 2 2015-04-14 00:53:45.359 FirstBook065[1147:82365] textFieldShouldBeginEditing: 通过NSLog确认delegate的设置 3 2015-04-14 00:53:45.367 FirstBook065[1147:82365] textFieldDidBeginEditing: 通过NSLog确认delegate的设置 4 2015-04-14 00:53:48.952 FirstBook065[1147:82365] textFieldShouldClear: 通过NSLog确认delegate的设置 5 2015-04-14 00:53:51.164 FirstBook065[1147:82365] shouldChangeCharactersInRange: w 6 2015-04-14 00:53:51.274 FirstBook065[1147:82365] shouldChangeCharactersInRange: e 7 2015-04-14 00:53:59.771 FirstBook065[1147:82365] textFieldShouldReturn: 为什么我 8 2015-04-14 00:54:04.832 FirstBook065[1147:82365] shouldChangeCharactersInRange: w 9 2015-04-14 00:54:05.242 FirstBook065[1147:82365] shouldChangeCharactersInRange: w 10 2015-04-14 00:54:14.043 FirstBook065[1147:82365] textFieldShouldReturn: 为什么我ww 11 2015-04-14 00:54:15.889 FirstBook065[1147:82365] Tap Gesture Recognizer,去掉某些控件作为第一响应器 12 2015-04-14 00:54:15.889 FirstBook065[1147:82365] textFieldShouldEndEditing: 为什么我ww 13 2015-04-14 00:54:15.892 FirstBook065[1147:82365] textFieldDidEndEditing: 为什么我ww
时间: 2024-11-05 23:20:13