鍍金池/ 教程/ iOS/ iOS - Text Field(文本域)
iOS - Switches(切換/開(kāi)關(guān))
iOS - Labels(標(biāo)簽)
iOS - Table View(表格視圖)
IOS - 攝像頭管理
iOS - Text View(文本視圖)
IOS - 開(kāi)發(fā)環(huán)境配置
iOS教程
iOS - Twitter & Facebook
iOS - UI元素
iOS - iAd 整合
IOS - 應(yīng)用程序調(diào)試
iOS - Split View(分割視圖)
iOS - Status Bar(狀態(tài)欄)
iOS - Navigation Bar(導(dǎo)航欄)
iOS - Tab bar(標(biāo)簽欄)
IOS - 文件處理
IOS - 自動(dòng)布局
iOS - Image View(圖像視圖)
iOS - 應(yīng)用程序內(nèi)購(gòu)買(mǎi)
iOS - Pickers(選取器)
iOS - Delegates實(shí)例
iOS - 創(chuàng)建第一個(gè)iPhone應(yīng)用
iOS (iPhone, iPad)教程
iOS - 發(fā)送電子郵箱(Email)
iOS - View Transitions(視圖轉(zhuǎn)換)
iOS - 內(nèi)存管理
iOS - Icons(圖標(biāo))
iOS - 音頻和視頻
iOS - Storyboards(演示圖板演)
iOS - Buttons(按鈕)
iOS - Text Field(文本域)
iOS - Sliders(滑動(dòng)條)
iOS - Scroll View(滾動(dòng)視圖)
IOS - 輸入類型 文本字段
iOS - 位置處理
iOS - Accelerometer(加速度傳感器)
IOS - 快速入門(mén)
iOS - SQLite 數(shù)據(jù)庫(kù)
iOS - GameKit
IOS - 訪問(wèn)地圖
iOS - Objective-C基礎(chǔ)
iOS - Toolbar(工具欄)
IOS - 動(dòng)作和插座(Outlets)
iOS - Alerts(警示)
IOS - 通用應(yīng)用程序

iOS - Text Field(文本域)

使用文本字段

一個(gè)文本字段是一個(gè)UI元素,使應(yīng)用程序獲取用戶輸入
UITextField 如下所示
iOS Text field

 

文本字段的重要屬性如下:

  • 不需要用戶輸入占位符文本時(shí)顯示

  • 普通文本

  • 自動(dòng)校正類型

  • 鍵盤(pán)類型

  • 返回鍵的類型

  • 清除按鈕模式

  • 對(duì)齊方式

  • 委托

更新屬性 xib

可以在 xib 更改文本字段屬性在屬性檢查在工具程序區(qū)(右側(cè)窗口)。

iOS Tutorial

文本字段的委托

我們可以設(shè)置在Interface Builder代表右擊UIElement的將它連接到文件的所有者,如下圖所示。

iOS Tutorial

 

使用委托的步驟

1. 如上圖所示,設(shè)置委托

2. 委托類響應(yīng)

3. 實(shí)現(xiàn) TextField 委托,最重要的文本字段委托如下


- (void)textFieldDidBeginEditing:(UITextField *)textField


- (void)textFieldDidEndEditing:(UITextField *)textField

4. 正如其名稱所義,一旦我們開(kāi)始編輯的文本字段和結(jié)束分別編輯上述兩個(gè)委托被調(diào)用。

5. 對(duì)于其他代表,請(qǐng)參閱UITextDelegate協(xié)議參考。

 

示例代碼和步驟

1. 我們將使用示例應(yīng)用程序創(chuàng)建UI元素

2. ViewController 類會(huì)采用 UITextFieldDelegate,更新如下我們的ViewController.h文件。

#import <UIKit/UIKit.h>

// You can notice the adddition of UITextFieldDelegate below
@interface ViewController : UIViewController<UITextFieldDelegate>

@end

3. 然后,我們添加了一個(gè)的方法addTextField 在 ViewController.m 文件。

4. 在 viewDidLoad方法中調(diào)用此方法。

5. 更新 ViewController.m 和  viewDidLoad 如下

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //The custom method to create our textfield is called
    [self addTextField];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addTextField{
   // This allocates a label 
   UILabel *prefixLabel = [[UILabel alloc]initWithFrame:CGRectZero];
   //This sets the label text
   prefixLabel.text =@"## ";
   // This sets the font for the label
   [prefixLabel setFont:[UIFont boldSystemFontOfSize:14]];
   // This fits the frame to size of the text
   [prefixLabel sizeToFit];
	
   // This allocates the textfield and sets its frame
   UITextField *textField = [[UITextField  alloc] initWithFrame:
   CGRectMake(20, 50, 280, 30)];
   
   // This sets the border style of the text field 
   textField.borderStyle = UITextBorderStyleRoundedRect;
   textField.contentVerticalAlignment =
   UIControlContentVerticalAlignmentCenter;
   [textField setFont:[UIFont boldSystemFontOfSize:12]];
   
   //Placeholder text is displayed when no text is typed
   textField.placeholder = @"Simple Text field";
   
   //Prefix label is set as left view and the text starts after that
   textField.leftView = prefixLabel;
  
   //It set when the left prefixLabel to be displayed
   textField.leftViewMode = UITextFieldViewModeAlways;
  
   // Adds the textField to the view.
   [self.view addSubview:textField];
  
   // sets the delegate to the current class
   textField.delegate = self;
}

// pragma mark is used for easy access of code in Xcode
#pragma mark - TextField Delegates

// This method is called once we click inside the textField
-(void)textFieldDidBeginEditing:(UITextField *)textField{
   NSLog(@"Text field did begin editing");
}

// This method is called once we complete editing
-(void)textFieldDidEndEditing:(UITextField *)textField{
   NSLog(@"Text field ended editing");
}

// This method enables or disables the processing of return key
-(BOOL) textFieldShouldReturn:(UITextField *)textField{    
    [textField resignFirstResponder];
    return YES;
}

- (void)viewDidUnload {
   label = nil;
   [super viewDidUnload];
}

@end

6. 現(xiàn)在,當(dāng)我們運(yùn)行程序時(shí),我們會(huì)得到下面的輸出。

iOS Tutorial

7. 委托方法被調(diào)用基于用戶操作。查看控制臺(tái)輸出,知道什么時(shí)候被調(diào)用委托。