アラートにテキストフィールドを追加する
ちょっとしたテキスト情報の入力とか、ID/Passの入力などわざわざviewをつくるよりアラートでできた方が便利ですね。
今回はそのやり方。
UIAlertView* alert = [UIAlertView new]; alert.title = @"タイトル"; alert.message = @"メッセージ"; alert.delegate = self; //<UIAlertViewDelegate>を設定する事 [alert addButtonWithTitle:@"cancel"]; [alert addButtonWithTitle:@"OK"]; alert.alertViewStyle = UIAlertViewStyleDefault; [alert textFieldAtIndex:0].placeholder = @"プレースホルダー"; [alert textFieldAtIndex:0].clearButtonMode = UITextFieldViewModeWhileEditing; [alert show];
alertViewStyle
プロパティには以下の3つが設定できます。
- UIAlertViewStylePlainTextInput
- UIAlertViewStyleLoginAndPasswordInput
- UIAlertViewStyleSecureTextInput
UIAlertViewStyleDefault
にすると普通のアラートが出てきます。(デフォルト)
それぞれのテキストフィールドの設定を行う場合は
[alert textFieldAtIndex:0]
[alert textFieldAtIndex:1]
といった感じで指定します。
上記の例だと、placeholderとclearButtonModeの設定を行っています。
入力した情報の取得は以下の通り
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex==1) { NSLog(@"text=%@",[[alertView textFieldAtIndex:0] text]); NSLog(@"text=%@",[[alertView textFieldAtIndex:1] text]); } }