中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

iOS – 文本字段(Text Field)

文本字段的使用

文本字段是一個(gè)用戶界面元素,通過應(yīng)用程序來獲取用戶輸入。

一個(gè)UITextfield如下所示:

textfeild

重要的文本字段的屬性

  • 在沒有任何用戶輸入時(shí),顯示占位符
  • 正常文本
  • 自動(dòng)更正型
  • 鍵盤類型
  • 返回鍵類型
  • 清除按鈕模式
  • 對(duì)齊方式
  • 委托

更新xib中的屬性

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

UITextField_Attribute


文本字段委托

我們可以通過右擊?UIElement?界面生成器中設(shè)置委托并將它連接到文件的所有者,如下所示:

TextfieldDelegate

使用委托的步驟:

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

實(shí)例

以下我們使用簡(jiǎn)單的實(shí)例來創(chuàng)建UI元素

ViewController 類將采用UITextFieldDelegate,修改ViewController.h文件,如下所示:

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

然后在 viewDidLoad 方法中調(diào)用此方法

在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

運(yùn)行該應(yīng)用程序會(huì)看到下面的輸出

TextfieldOutput

委托調(diào)用的方法基于用戶操作。要知道調(diào)用委托時(shí)請(qǐng)參閱控制臺(tái)輸出。

其他擴(kuò)展