iOS学习-UITextField代理

  1 //
  2 //  ViewController.m
  3 //  UITextField代理
  4 //
  5 //  Created by 大欢 on 16/1/22.
  6 //  Copyright © 2016年 bjsxt. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 
 11 @interface ViewController ()<UITextFieldDelegate>
 12 
 13 @property (nonatomic, strong) UITextField * userTF;
 14 
 15 @property (nonatomic, strong) UITextField * pwdTF;
 16 
 17 @end
 18 
 19 @implementation ViewController
 20 
 21 - (void)viewDidLoad {
 22     [super viewDidLoad];
 23     
 24     UITextField * userTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, CGRectGetWidth(self.view.frame) - 40, 50)];
 25     userTextField.placeholder = @"请输入用户名";
 26     userTextField.borderStyle = UITextBorderStyleRoundedRect;
 27     userTextField.delegate = self;
 28     userTextField.clearButtonMode = UITextFieldViewModeAlways;
 29     userTextField.returnKeyType = UIReturnKeyNext;
 30     [self.view addSubview:userTextField];
 31     self.userTF = userTextField;
 32     
 33     UITextField * pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(userTextField.frame) + 20, CGRectGetWidth(self.view.frame) - 40, 50)];
 34     pwdTextField.placeholder = @"请输入密码";
 35     pwdTextField.borderStyle = UITextBorderStyleRoundedRect;
 36     pwdTextField.delegate = self;
 37     pwdTextField.returnKeyType = UIReturnKeyGo;
 38     [self.view addSubview:pwdTextField];
 39     self.pwdTF = pwdTextField;
 40     
 41     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidBeginEditingNotification:) name:UITextFieldTextDidBeginEditingNotification object:nil];
 42     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidEndEditingNotification:) name:UITextFieldTextDidEndEditingNotification object:nil];
 43     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:nil];
 44     
 45 }
 46 
 47 - (void)textDidBeginEditingNotification:(NSNotification *)notifi {
 48     NSLog(@"开始");
 49 }
 50 
 51 - (void)textDidEndEditingNotification:(NSNotification *)notifi {
 52     NSLog(@"结束");
 53 }
 54 
 55 - (void)textDidChangeNotification:(NSNotification *)notifi {
 56     
 57     UITextField * tf = notifi.object;
 58     NSLog(@"%@",tf.text);
 59 }
 60 
 61 //是否可以开始编辑
 62 
 63 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
 64     
 65     return YES;
 66 }
 67 
 68 //是否可以结束编辑
 69 
 70 //- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
 71 //    
 72 //    return YES;
 73 //}
 74 //
 75 //- (void)textFieldDidBeginEditing:(UITextField *)textField {
 76 //    
 77 //    NSLog(@"已经开始");
 78 //}
 79 //
 80 //- (void)textFieldDidEndEditing:(UITextField *)textField {
 81 //    
 82 //    NSLog(@"已经结束");
 83 //}
 84 //
 85 //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
 86 //    
 87 //    if (range.location > 2) return NO;
 88 //    
 89 //    NSLog(@"%@",NSStringFromRange(range));
 90 //    
 91 //    NSLog(@"%@",string);
 92 //    
 93 //    
 94 //    return YES;
 95 //}
 96 
 97 //clearbutton 如果是NO不好使
 98 
 99 //- (BOOL)textFieldShouldClear:(UITextField *)textField {
100 //    
101 //    return NO;
102 //}
103 
104 
105 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
106     
107     if (self.userTF == textField) {
108         [self.pwdTF becomeFirstResponder];
109     } else if (self.pwdTF == textField) {
110         [self.pwdTF resignFirstResponder];
111     }
112     
113     return YES;
114 }
115 
116 
117 
118 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
119     
120     [self.userTF resignFirstResponder];
121     
122     [self.pwdTF resignFirstResponder];
123     
124 }
125 
126 
127 @end

 

posted @ 2016-01-25 14:01  MrWuYindi  阅读(216)  评论(0编辑  收藏  举报