iOS - Text Field(文本域)

使用文本字段

一個文本字段是一個UI元素,使應用程式獲取用戶輸入
UITextField 如下所示
iOS Text field

 

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

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

  • 普通文本

  • 自動校正類型

  • 鍵盤類型

  • 返回鍵的類型

  • 清除按鈕模式

  • 對齊方式

  • 委託

更新屬性 xib

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

iOS Tutorial

文本字段的委託

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

iOS Tutorial

 

使用委託的步驟

1. 如上圖所示,設置委託

2. 委託類回應

3. 實現 TextField 委託,最重要的文本字段委託如下


- (void)textFieldDidBeginEditing:(UITextField *)textField


- (void)textFieldDidEndEditing:(UITextField *)textField

4. 正如其名稱所義,一旦我們開始編輯的文本字段和結束分別編輯上述兩個委託被調用。

5. 對於其他代表,請參閱UITextDelegate協議參考。

 

示例代碼和步驟

1. 我們將使用示例應用程式創建UI元素

2. ViewController 類會採用 UITextFieldDelegate,更新如下我們的ViewController.h檔。

#import <UIKit/UIKit.h>

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

@end

3. 然後,我們添加了一個的方法addTextField 在 ViewController.m 檔。

4. 在 viewDidLoad方法中調用此方法。

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. 現在,當我們運行程式時,我們會得到下麵的輸出。

iOS Tutorial

7. 委託方法被調用基於用戶操作。查看控制臺輸出,知道什麼時候被調用委託。


上一篇: iOS - UI元素 下一篇: IOS - 輸入類型 文本字段