文本字段的使用

文本字段是一個用戶介面元素,通過應用程式來獲取用戶輸入。

一個UITextfield如下所示:

textfeild

重要的文本字段的屬性

  • 在沒有任何用戶輸入時,顯示占位符
  • 正常文本
  • 自動更正型
  • 鍵盤類型
  • 返回鍵類型
  • 清除按鈕模式
  • 對齊方式
  • 委託

更新xib中的屬性

可以在Utility area(實用區域,窗口的右側)更改xib在屬性查看器中的文本字段屬性。

UITextField_Attribute


文本字段委託

我們可以通過右擊 UIElement 介面生成器中設置委託並將它連接到檔的所有者,如下所示:

TextfieldDelegate

使用委託的步驟:

  • 1.設置委託如上圖所示
  • 2.添加委託到您的回應類
  • 3.執行文本字段代表,重要的文本字段代表如下:
  • - (void)textFieldDidBeginEditing:(UITextField *)textField
    
    - (void)textFieldDidEndEditing:(UITextField *)textField
    
  • 4.正如其名稱所暗示,上述兩個委託分別叫做編輯的文本字段和結束編輯
  • 5. 其他的委託請查看 UITextDelegate Protocol 參考手冊。


實例

以下我們使用簡單的實例來創建UI元素

ViewController 類將採用UITextFieldDelegate,修改ViewController.h檔,如下所示:

將方法addTextField添加到我們的 ViewController.m 檔

然後在 viewDidLoad 方法中調用此方法

在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

運行該應用程式會看到下麵的輸出

TextfieldOutput

委託調用的方法基於用戶操作。要知道調用委託時請參閱控制臺輸出。