收回软键盘的两种方式

 

来源:http://www.open-open.com/lib/view/open1341882439838.html

这次讲的内容很简单:

1.首先我们还是创建一个Single View Application,然后打开MainStoryboard_iphone.storyboard,在里面放入俩lable和两个TextFiled:

ios学习笔记(四)收回软键盘的两种方式

2.接着开始写代码:ViewController.h:

 1 #import <uikit uikit.h="">

 2 

 3 @interface ViewController : UIViewController{

 4 UITextField *nameField;

 5 UITextField *numberField;

 6 }

 7 @property (nonatomic,retain) IBOutlet UITextField *nameField;

 8 @property (nonatomic,retain) IBOutlet UITextField *numberField;

 9 - (IBAction)backgroundTap:(id)sender;

10 - (IBAction)textFiledReturnEditing:(id)sender;

11 @end</uikit>

ViewController.m:

 1 #import "ViewController.h"

 2 

 3 @interface ViewController ()

 4 

 5 @end

 6 

 7 @implementation ViewController

 8 @synthesize nameField;

 9 @synthesize numberField;

10 - (void)viewDidLoad

11 {

12     [super viewDidLoad];

13     // Do any additional setup after loading the view, typically from a nib.

14 }

15 

16 - (void)viewDidUnload

17 {

18     [super viewDidUnload];

19     // Release any retained subviews of the main view.

20 }

21 //点击屏幕空白view时触发的事件

22 - (IBAction)backgroundTap:(id)sender{

23     

24     [nameField resignFirstResponder];//通知文本失去第一响应者状态

25     [numberField resignFirstResponder];

26 }

27 

28 //点击return时触发的事件?

29 - (IBAction)textFiledReturnEditing:(id)sender {

30     [sender resignFirstResponder];

31 }

32 

33 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

34 {

35     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

36         return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

37     } else {

38         return YES;

39     }

40 }

41 @end

3.接着我们连接操作和输出口:

将背景view的类别设置为UIControl,这样我们就能对屏幕的事件进行处理了,将Control的touch down输出连接到backgroundTap事件上,因为点击软键盘会触发did end on exit,那我们就把两个textFiled的did end on exit输出连接到textFiledReturnEditing事件上。当然我们不要忘记将两个textFiled控件的输出与 ViewController的相应控件接口连接在一起。

4.运行程序看看效果:

点击textFiled时:

ios学习笔记(四)收回软键盘的两种方式

 

点击return或点击界面空白时:

ios学习笔记(四)收回软键盘的两种方式

转自:http://blog.csdn.net/itachi85/article/details/7664563

你可能感兴趣的:(软键盘)