Ios-ui-elements-text-field
提供:Dev Guides
iOS-テキストフィールド
テキストフィールドの使用
テキストフィールドは、アプリがユーザー入力を取得できるようにするUI要素です。
UITextfieldを以下に示します。
テキストフィールドの重要なプロパティ
- ユーザー入力がないときに表示されるプレースホルダーテキスト
- 通常のテキスト
- 自動補正タイプ
- キーボードタイプ
- リターンキータイプ
- クリアボタンモード
- アライメント
- 委任
xibでプロパティを更新する
ユーティリティ領域(ウィンドウの右側)の属性インスペクターでxibのテキストフィールドプロパティを変更できます。
テキストフィールドデリゲート
以下に示すように、UIElementを右クリックしてファイル所有者に接続することにより、インターフェイスビルダーでデリゲートを設定できます。
デリゲートを使用する手順
- ステップ1 *-上図のようにデリゲートを設定します。
- ステップ2 *-クラスが応答するデリゲートを追加します。
- ステップ3 *-textFieldデリゲートを実装します。重要なテキストフィールドデリゲートは次のとおりです-
- (void)textFieldDidBeginEditing:(UITextField *)textField
- (void)textFieldDidEndEditing:(UITextField *)textField
- ステップ4 *-名前が示すように、テキストフィールドの編集を開始し、編集を終了すると、上記の2つのデリゲートがそれぞれ呼び出されます。
- ステップ5 *-他のデリゲートについては、UITextDelegate Protocolリファレンスを参照してください。
サンプルコードと手順
- ステップ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 *-次に、ViewController.mファイルにメソッド *addTextField を追加します。
- ステップ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 *-アプリケーションを実行すると、次の出力が得られます。
- ステップ7 *-デリゲートメソッドは、ユーザーアクションに基づいて呼び出されます。 コンソール出力を参照して、デリゲートがいつ呼び出されるかを確認してください。