键盘下落123

键盘下落第一种方法:输入框绑定方法


UITextField *textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];

textfield.borderStyle=UITextBorderStyleRoundedRect;

textfield.placeholder=@"第一种方法";

textfield.clearButtonMode=UITextFieldViewModeWhileEditing;

textfield.backgroundColor= [UIColor magentaColor];

//键盘下去第一种方法:输入框绑定方法
//UIControlEventEditingDidEndOnExit编辑结束并退出
//点击return键键盘下去绑定的方法中可以什么都不写

[textfield addTarget:self action:@selector(keyboardDown) forControlEvents:UIControlEventEditingDidEndOnExit];

[self.view addSubview:textfield];

//键盘下落第二种方法:失去第一响应者

//创建一个control点击control键盘下去

UIControl *control = [[[UIControl alloc] initWithFrame:[UIScreen mainScreen] bounds]:

[control addTarget:self action:@selector(controlTouched) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:control];

//创建一个输入框

UITextField *textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];

//设置tag值,方便等下找到并针对这个输入框的操作

textfield.tag = 30;

textfield.borderStyle=UITextBorderStyleRoundedRect;

textfield.placeholder=@"第二种方法";

textfield.clearButtonMode=UITextFieldViewModeWhileEditing;

textfield.backgroundColor= [UIColor magentaColor];

[self.view addSubview:textfield];

//程序运行就让键盘上来这个时候输入框第一响应者
//becomeFirstResponder成为第一响应者

[textfield becomeFirstResponder];

//实现control绑定的方法

-(void)controlTouched {

//让输入框失去第一响应者

UITextField *textfield=[self.view viewWithTag:30];

//resignFirstResponder失去第一响应者

[textfield resignFirstResponder];

}

//键盘下落第三种方法:touchesBegan

//创建一个输入框

UITextField *textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];

textfield.borderStyle=UITextBorderStyleRoundedRect;

textfield.placeholder=@"第三种方法";

textfield.clearButtonMode=UITextFieldViewModeWhileEditing;

textfield.backgroundColor= [UIColor magentaColor];

[self.view addSubview:textfield];

//使用系统的触摸方法,在触摸开始的时候调用

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

//endEditing是否强制结束编辑

[self.view endEditing:YES];

}

你可能感兴趣的:(键盘下落123)