iPhone UI設計註冊頁面的實現及TextField關閉鍵盤的兩種方法

在Xcode4.3.2中,我們新建一個IOS CocoaTouch項目,命名爲:register。在ViewController.h文件中定義四個輸出口:user,pass,year,sex;

Label因爲不需要獲取數據所以可以不定義輸出口,定義兩個Button按鈕:Cancal,ok;

在ViewController.h中定義如下:

[plain]
// 
//  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]
// 
//  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]
-(IBAction)TextFieldDoneEditing:(id)sender; 
在ViewController.m中實現此方法:

[plain]
-(void)TextFieldDoneEditing:(id)sender 

    [sender resignFirstResponder]; 

然後讓四個TextField的Did End on Exit方法連接到TextFieldDoneEditing方法上即可實現通過軟鍵盤return關閉鍵盤功能。

由於我們輸入的信息不同,激活的鍵盤格式也不一樣,比如說Number key就是沒有return鍵的,那麼我們如何關閉這樣的鍵盤呢?

我們在ViewController.h中添加一個新的方法:

[plain]
-(IBAction)BackgroundClick:(id)sender; 

在ViewController.m中實現:

[plain]
-(void)BackgroundClick:(id)sender 

    [textuser resignFirstResponder]; 
    [textpass resignFirstResponder]; 
    [textsex resignFirstResponder]; 
    [textyear resignFirstResponder]; 

把每個textField都添加進去,然後在每個TextField的touch up inside方法連接到BackgroundClick方法上即可。

這樣,我們輸完內容後,點擊非活動背景即可關閉鍵盤,大家嘗試一下吧。有什麼問題給我留言,謝謝

發佈了15 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章