iPhone UI设计注册页面的实现及TextField关闭键盘的两种方法实例源码



iPhone UI设计注册页面的实现及TextField关闭键盘的两种方法实例源码。  在Xcode4.3.2中,我们新建一个IOS CocoaTouch项目,命名为:register。在ViewController.h文件中定义四个输出口:user,pass,year,sex;

Label因为不需要获取数据所以可以不定义输出口,定义两个Button按钮:Cancal,ok;

在ViewController.h中定义如下:

 

[plain] view plaincopy

  1. //
  2. //  ViewController.h
  3. //  register
  4. //
  5. //  Created by bo yang on 5/10/12.
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. @interface ViewController : UIViewController
  10. {
  11.     UIButton *cancal;
  12.     UIButton *ok;
  13.     UITextField *textuser;
  14.     UITextField *textpass;
  15.     UITextField *textsex;
  16.     UITextField *year;
  17. }
  18. @property IBOutlet UIButton *cancal;
  19. @property IBOutlet UIButton *ok;
  20. @property IBOutlet UITextField *textuser;
  21. @property IBOutlet UITextField *textpass;
  22. @property IBOutlet UITextField *textsex;
  23. @property IBAction UITextField *year;
  24. @end

在头文件和实现文件中分别实现存储器功能:

 

 

[plain] view plaincopy

  1. //
  2. //  ViewController.m
  3. //  register
  4. //
  5. //  Created by bo yang on 5/10/12.
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import ”ViewController.h”
  9. @interface ViewController ()
  10. @end
  11. @implementation ViewController
  12. @synthesize cancal;
  13. @synthesize ok;
  14. @synthesize textuser;
  15. @synthesize textpass;
  16. @synthesize textsex;
  17. - (void)viewDidLoad
  18. {
  19.     [super viewDidLoad];
  20.     // Do any additional setup after loading the view, typically from a nib.
  21. }
  22. - (void)viewDidUnload
  23. {
  24.     [super viewDidUnload];
  25.     // Release any retained subviews of the main view.
  26. }
  27. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  28. {
  29.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  30. }
  31. @end

然后我们在ViewController.xib文件中设计UI界面:

 

“register”为Label标签,修改了字体的大小和颜色;

添加了一个背景;

Label:user,pass,sex,year;

Button:Cancal,Ok

然后我们实现关闭键盘的方法:

首先在头文件ViewController.h中添加一个方法:

 

[plain] view plaincopy


  1. -(IBAction)TextFieldDoneEditing:(id)sender;

在ViewController.m中实现此方法:

 

 

[plain] view plaincopy

  1. -(void)TextFieldDoneEditing:(id)sender
  2. {
  3.     [sender resignFirstResponder];
  4. }

然后让四个TextField的Did End on Exit方法连接到TextFieldDoneEditing方法上即可实现通过软键盘return关闭键盘功能。

 

由于我们输入的信息不同,激活的键盘格式也不一样,比如说Number key就是没有return键的,那么我们如何关闭这样的键盘呢?

我们在ViewController.h中添加一个新的方法:

 

[plain] view plaincopy

  1. -(IBAction)BackgroundClick:(id)sender;

在ViewController.m中实现:

 

 

[plain] view plaincopy

  1. -(void)BackgroundClick:(id)sender
  2. {
  3.     [textuser resignFirstResponder];
  4.     [textpass resignFirstResponder];
  5.     [textsex resignFirstResponder];
  6.     [textyear resignFirstResponder];
  7. }

把每个textField都添加进去,然后在每个TextField的touch up inside方法连接到BackgroundClick方法上即可。

 

这样,我们输完内容后,点击非活动背景即可关闭键盘,大家尝试一下吧。有什么问题给我留言,谢谢。