1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| UITextField*textfield = [[UITextField alloc]initWithFrame:CGRectMake(20,20,200,40)]; [self.window addSubview:self.myTextField]; self.myTextField.backgroundColor = [UIColor clearColor]; self.myTextField.borderStyle = UITextBorderStyleRoundedRect; self.myTextField.secureTextEntry = YES; self.myTextField.placeholder = @"请输入"; self.myTextField.clearButtonMode = UITextFieldViewModeAlways; self.myTextField.keyboardType = UIKeyboardTypeNumberPad;
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom]; [self.window addSubview:Button]; Button.frame = CGRectMake(20, 80, 100, 30); Button.backgroundColor = [UIColor cyanColor];
[Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
-(void) buttonAction:(UIButton *)b { [self.myTextField resignFirstResponder]; }
1.签UITextFieldDelegate协议 @interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>
2.成为代理人 哪个输入框成为代理人哪个输入框有此功能 self.myTextField.delegate = self;
3.实现协议里的方法
通用,一个就可以给所有输入框用 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [self.myTextField resignFirstResponder]; return YES;
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backgroundTap:)]; [self.view addGestureRecognizer:tap];
- (IBAction)backgroundTap:(id)sender { [self.studentDetailV.tf1 resignFirstResponder]; [self.studentDetailV.tf2 resignFirstResponder]; [self.studentDetailV.tf3 resignFirstResponder]; [self.studentDetailV.tf4 resignFirstResponder]; [self.studentDetailV.tf5 resignFirstResponder]; }
|