ios实现简单的键盘回收的方法
2015-11-22 20:07
609 查看
第一步:需要给控制器添加代理协议:(注意 如果没有添加代理协议 则无法添加系统提供的return 回收键盘的方法)
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic, retain) UITextField *textField;
@end
第二步:将textField的代理设置给当前控制器
UITextField *textEmail=[[UITextField alloc] initWithFrame:CGRectMake(150, 255, 160, 30)];
//为了调用系统的键盘回收的方法
textEmail.delegate=self;
//为了调用单击事件的方法
self.textField=textEmail;
//通过一个按钮来实现键盘回收的方法
UIButton *login=[[UIButton alloc] initWithFrame:CGRectMake(120, 290, 60,50)];
给按钮添加单击事件
[login addTarget:self action:@selector(zhuce:) forControlEvents:UIControlEventTouchUpInside];
第三步:添加系统提供的键盘回收的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
这样就可以实现 点击键盘上的return 回收 键盘的目的
//给按钮注册一个单击事件
-(void)zhuce:(UIButton *)btn{
[self.textField resignFirstResponder];
}
这样单击注册按钮时可以 收回键盘
再介绍一种点击空白 就能实现回收键盘的简单方法 不需要 添加代理协议 直接写即可
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic, retain) UITextField *textField;
@end
第二步:将textField的代理设置给当前控制器
UITextField *textEmail=[[UITextField alloc] initWithFrame:CGRectMake(150, 255, 160, 30)];
//为了调用系统的键盘回收的方法
textEmail.delegate=self;
//为了调用单击事件的方法
self.textField=textEmail;
//通过一个按钮来实现键盘回收的方法
UIButton *login=[[UIButton alloc] initWithFrame:CGRectMake(120, 290, 60,50)];
给按钮添加单击事件
[login addTarget:self action:@selector(zhuce:) forControlEvents:UIControlEventTouchUpInside];
第三步:添加系统提供的键盘回收的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
这样就可以实现 点击键盘上的return 回收 键盘的目的
//给按钮注册一个单击事件
-(void)zhuce:(UIButton *)btn{
[self.textField resignFirstResponder];
}
这样单击注册按钮时可以 收回键盘
再介绍一种点击空白 就能实现回收键盘的简单方法 不需要 添加代理协议 直接写即可
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
相关文章推荐
- iOS开发Post请求错误:Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or ...
- IOS中输入框被软键盘遮挡的解决办法
- iOS 7应用开发公开课笔记 L13:Core Data and Table View
- ios 通知机制
- iOS 7应用开发公开课笔记 L12:Documents and Core Data
- iOS 7应用开发公开课笔记 L11:Table View and iPad
- iOS 7应用开发公开课笔记 L10:Multithreading, Scroll View
- iOS 7应用开发公开课笔记 L9:Animation and Autolayout
- iOS 7应用开发公开课笔记 L8:Protocols, Blocks, and Animation
- iOS 7应用开发公开课笔记 L7:Views and Gestures
- iOS 7应用开发公开课笔记 L4:Foundation and Attributed Strings
- iOS - 推送
- 现有iOS设备
- iOS---限制文本框输入的长度
- ios的crash分析工具
- iOS中的隐式动画
- 周末了,给iOS开发的孩子们的鸡汤——阿飞
- iOS开发41-XMPP和XAMPP的简介
- 修改mjextension使用一键归档在iOS9下有一定几率会闪退的bug
- POST