iPhone UI设计注册页面的实现及TextField关闭键盘的两种方法实例源码。 在Xcode4.3.2中,我们新建一个IOS CocoaTouch项目,命名为:register。在ViewController.h文件中定义四个输出口:user,pass,year,sex;
Label因为不需要获取数据所以可以不定义输出口,定义两个Button按钮:Cancal,ok;
在ViewController.h中定义如下:
[plain] view plaincopy
- //
 - // ViewController.h
 - // register
 - //
 - // Created by bo yang on 5/10/12.
 - // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
 - //
 - #import <UIKit/UIKit.h>
 - @interface ViewController : UIViewController
 - {
 - UIButton *cancal;
 - UIButton *ok;
 - UITextField *textuser;
 - UITextField *textpass;
 - UITextField *textsex;
 - UITextField *year;
 - }
 - @property IBOutlet UIButton *cancal;
 - @property IBOutlet UIButton *ok;
 - @property IBOutlet UITextField *textuser;
 - @property IBOutlet UITextField *textpass;
 - @property IBOutlet UITextField *textsex;
 - @property IBAction UITextField *year;
 - @end
 
在头文件和实现文件中分别实现存储器功能:
[plain] view plaincopy
- //
 - // ViewController.m
 - // register
 - //
 - // Created by bo yang on 5/10/12.
 - // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
 - //
 - #import ”ViewController.h”
 - @interface ViewController ()
 - @end
 - @implementation ViewController
 - @synthesize cancal;
 - @synthesize ok;
 - @synthesize textuser;
 - @synthesize textpass;
 - @synthesize textsex;
 - - (void)viewDidLoad
 - {
 - [super viewDidLoad];
 - // Do any additional setup after loading the view, typically from a nib.
 - }
 - - (void)viewDidUnload
 - {
 - [super viewDidUnload];
 - // Release any retained subviews of the main view.
 - }
 - - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 - {
 - return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
 - }
 - @end
 
然后我们在ViewController.xib文件中设计UI界面:

“register”为Label标签,修改了字体的大小和颜色;
添加了一个背景;
Label:user,pass,sex,year;
Button:Cancal,Ok
然后我们实现关闭键盘的方法:
首先在头文件ViewController.h中添加一个方法:
[plain] view plaincopy
- -(IBAction)TextFieldDoneEditing:(id)sender;
 
在ViewController.m中实现此方法:
[plain] view plaincopy
- -(void)TextFieldDoneEditing:(id)sender
 - {
 - [sender resignFirstResponder];
 - }
 
然后让四个TextField的Did End on Exit方法连接到TextFieldDoneEditing方法上即可实现通过软键盘return关闭键盘功能。
由于我们输入的信息不同,激活的键盘格式也不一样,比如说Number key就是没有return键的,那么我们如何关闭这样的键盘呢?
我们在ViewController.h中添加一个新的方法:
[plain] view plaincopy
- -(IBAction)BackgroundClick:(id)sender;
 
在ViewController.m中实现:
[plain] view plaincopy
- -(void)BackgroundClick:(id)sender
 - {
 - [textuser resignFirstResponder];
 - [textpass resignFirstResponder];
 - [textsex resignFirstResponder];
 - [textyear resignFirstResponder];
 - }
 
把每个textField都添加进去,然后在每个TextField的touch up inside方法连接到BackgroundClick方法上即可。
这样,我们输完内容后,点击非活动背景即可关闭键盘,大家尝试一下吧。有什么问题给我留言,谢谢。