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

來源:互聯網
上載者:User

在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方法上即可。

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



摘自 安諾的專欄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.