Iphone开发(四)文本框,文本视图,和软键盘的隐藏

holydancer原创,如需转载,请在显要位置注明:

转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7396207

今天介绍几个基本控件和软键盘的操作,在iphone应用中用到一些文本编缉时,软键盘不会像android那样,在输入完成后点返回键自动隐藏,需要你写代码实现,所以键盘的隐藏也算是iphone开发的一个基础了。iphone开发中的文本框UITextField类似于android中的EditText,可以用于输入内容,而文本视图UITextView继承自UIScrollView类,可以进行滚动,字体编辑等,我们可以这样理解,UITextFiled可以用于短信界面的联系人输入,UITextView可以用于短信界面的短信内容输入。好吧,既然这样说了,今天我们就以一个模拟的短信发送界面来介绍一下文本框和文本视图,以及如何处理键盘的隐藏。

首先新建一个项目,然后打开xib文件,进行一下界面的构建:

拖好后我们先不进行代码的工作,先来看一下UITextField的属性栏,如下图:

text表示初始化时自动出现在框中的文字,placeholder表示隐藏提示,如图所示,类似于android中的hint,还有一些背景设置,对齐格式,边框样式,字体大小设置等,往下还有透明度等,截图中没有截上,总之就是一些常用的属性,注意Keyboard一栏可以设置键盘的格式,图中所示是只能输入数字的键盘。在UITextView中也与此差不多,但是多了这么一个属性栏 :

这个属性栏是设置UITextView的滚动属性的,一般不用做太大的修改。好了,下面我们来进行view和controller的连接,因为我们不打算对两个label和button进行操作,所以我们就不对他们进行输出口的声明了,仅仅声明UITextField和UITextView的输出口。如图所示,这样我们就可以对其在代码中进行操作。

viewController.h:

 

[plain]  view plain copy
 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. @property (retain, nonatomic) IBOutlet UITextField *myTextField;  
  5. @property (retain, nonatomic) IBOutlet UITextView *myTextView;  
  6.   
  7. @end  


viewController.m:

 

 

[html]  view plain copy
 
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8. @synthesize myTextField;  
  9. @synthesize myTextView;  
  10.   
  11. - (void)viewDidLoad  
  12. {  
  13.     [super viewDidLoad];  
  14.     // Do any additional setup after loading the view, typically from a nib.  
  15. }  
  16.   
  17. - (void)viewDidUnload  
  18. {  
  19.     [self setMyTextField:nil];  
  20.     [self setMyTextView:nil];  
  21.     [super viewDidUnload];  
  22.     // Release any retained subviews of the main view.  
  23. }  
  24.   
  25. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  26. {  
  27.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  28. }  
  29.   
  30. - (void)dealloc {  
  31.     [myTextField release];  
  32.     [myTextView release];  
  33.     [super dealloc];  
  34. }  
  35. @end  


这时运行模拟器我们会发现在点击文本框时会自动调出软键盘,但是输入完成后,该键盘不会自动关闭,这时我们需要点击xib文件中的第三个图标View,这是当前程序的主视图,然后如下图所示,将class改为UIControl,这时,整个大的视图就可以响应方法了。

 

通过更改UIView为UIControl,我们可以在触摸屏幕上非活动控件时都会产生Touch Down事件,我们需要在类中定义一个IBAction方法用以对应这个事件,在产生Touch Down事件时,会调用这个方法,然后该方法的实现中失去两个文本编辑框的响应者身份,代码为:

[ myTextField resignFirstResponder];

[myTextView resignFirstResponder];

这样在输入完成后,触摸屏幕上的其他地方,不管两个编辑框谁是当前的第一响应者,都会失去焦点,然后软键盘会自动关闭。下在我们先进行IBAction方法的实现,我们起名叫closeType方法:

viewController.h:

 

[plain]  view plain copy
 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. @property (retain, nonatomic) IBOutlet UITextField *myTextField;  
  5. @property (retain, nonatomic) IBOutlet UITextView *myTextView;  
  6. -(IBAction)closeType:(id)sender;  
  7. @end  


viewController.m:

 

 

[plain]  view plain copy
 
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8. @synthesize myTextField;  
  9. @synthesize myTextView;  
  10. -(IBAction)closeType:(id)sender  
  11. {  
  12.     [myTextView resignFirstResponder];  
  13.     [myTextField resignFirstResponder];  
  14. }  
  15. - (void)viewDidLoad  
  16. {  
  17.     [super viewDidLoad];  
  18.     // Do any additional setup after loading the view, typically from a nib.  
  19. }  
  20.   
  21. - (void)viewDidUnload  
  22. {  
  23.     [self setMyTextField:nil];  
  24.     [self setMyTextView:nil];  
  25.     [super viewDidUnload];  
  26.     // Release any retained subviews of the main view.  
  27. }  
  28.   
  29. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  30. {  
  31.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  32. }  
  33.   
  34. - (void)dealloc {  
  35.     [myTextField release];  
  36.     [myTextView release];  
  37.     [super dealloc];  
  38. }  
  39. @end  


然后viewController中有了一个可以和xib文件进行对接的方法,在xib文件中找到touch down方法,拖拽到左边的File's Owner,点击出现的closeType方法,这样在点击屏幕中空闲地方时,会触动touch down行为,然后呢,会调用viewController中的closeType方法,将当前的第一响应者身份取消,这样软键盘就会消失。

 

 

关键字: IOS ,Iphone 开发, iphone入门 ,iphone 文本框 , 文本视图 ,隐藏软键盘 .

猜你喜欢

转载自lishuaishuai.iteye.com/blog/1947505