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

天介绍几个基本控件和软键盘的操作,在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 plaincopyprint?
  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 
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *myTextField;
@property (retain, nonatomic) IBOutlet UITextView *myTextView;

@end

viewController.m:

 

 

[html] view plaincopyprint?
  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.     [super viewDidLoad]; 
  13.     // Do any additional setup after loading the view, typically from a nib. 
  14.  
  15. - (void)viewDidUnload 
  16.     [self setMyTextField:nil]; 
  17.     [self setMyTextView:nil]; 
  18.     [super viewDidUnload]; 
  19.     // Release any retained subviews of the main view. 
  20.  
  21. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
  22.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
  23.  
  24. - (void)dealloc { 
  25.     [myTextField release]; 
  26.     [myTextView release]; 
  27.     [super dealloc]; 
  28. @end 
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize myTextField;
@synthesize myTextView;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setMyTextField:nil];
    [self setMyTextView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    [myTextField release];
    [myTextView release];
    [super dealloc];
}
@end

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

 

 

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

[ myTextField resignFirstResponder];

[myTextView resignFirstResponder];

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

viewController.h:

 

[plain] view plaincopyprint?
  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 
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *myTextField;
@property (retain, nonatomic) IBOutlet UITextView *myTextView;
-(IBAction)closeType:(id)sender;
@end

viewController.m:

 

 

[plain] view plaincopyprint?
  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.     [myTextView resignFirstResponder]; 
  12.     [myTextField resignFirstResponder]; 
  13. - (void)viewDidLoad 
  14.     [super viewDidLoad]; 
  15.     // Do any additional setup after loading the view, typically from a nib. 
  16.  
  17. - (void)viewDidUnload 
  18.     [self setMyTextField:nil]; 
  19.     [self setMyTextView:nil]; 
  20.     [super viewDidUnload]; 
  21.     // Release any retained subviews of the main view. 
  22.  
  23. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
  24.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
  25.  
  26. - (void)dealloc { 
  27.     [myTextField release]; 
  28.     [myTextView release]; 
  29.     [super dealloc]; 
  30. @end 
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize myTextField;
@synthesize myTextView;
-(IBAction)closeType:(id)sender
{
    [myTextView resignFirstResponder];
    [myTextField resignFirstResponder];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setMyTextField:nil];
    [self setMyTextView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    [myTextField release];
    [myTextView release];
    [super dealloc];
}
@end

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

 

 

 

posted on 2012-11-08 18:07  水至清则无鱼  阅读(243)  评论(0编辑  收藏  举报

导航