`

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.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface ViewController : UIViewController  
  12.   
  13. {  
  14.     UIButton *cancal;  
  15.     UIButton *ok;  
  16.     UITextField *textuser;  
  17.     UITextField *textpass;  
  18.     UITextField *textsex;  
  19.     UITextField *year;  
  20. }  
  21. @property IBOutlet UIButton *cancal;  
  22. @property IBOutlet UIButton *ok;  
  23. @property IBOutlet UITextField *textuser;  
  24. @property IBOutlet UITextField *textpass;  
  25. @property IBOutlet UITextField *textsex;  
  26. @property IBAction UITextField *year;  
  27. @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.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16. @synthesize cancal;  
  17. @synthesize ok;  
  18. @synthesize textuser;  
  19. @synthesize textpass;  
  20. @synthesize textsex;  
  21.   
  22.   
  23. - (void)viewDidLoad  
  24. {  
  25.     [super viewDidLoad];  
  26.     // Do any additional setup after loading the view, typically from a nib.  
  27. }  
  28.   
  29. - (void)viewDidUnload  
  30. {  
  31.     [super viewDidUnload];  
  32.     // Release any retained subviews of the main view.  
  33. }  
  34.   
  35. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  36. {  
  37.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  38. }  
  39.   
  40. @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方法上即可。

 

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

0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics