2016-07-21 12 views
3

アラートにテキスト入力フィールドを表示して、ユーザーからの入力を取得し、その入力をアプリケーションで使用する(ラベルに入力を表示する)方法を教えてください。テキストフィールドエントリでアラートを表示

enter image description here enter image description here

下に示すようお時間をいただき、ありがとうございます。 UIAlertViewにテキストフィールドを追加する

答えて

11
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Text" 
                   message:@"Enter some text below" 
                 preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction *submit = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction * action) { 

                if (alert.textFields.count > 0) { 

                 UITextField *textField = [alert.textFields firstObject]; 

                 textField.text // your text 
                } 

               }]; 

[alert addAction:submit]; 

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
    textField.placeholder = @"something"; // if needs 
}]; 

[self presentViewController:alert animated:YES completion:nil]; 
1

プロトコルとしてUIAlertViewDelegateを追加し.Mファイルにデリゲート方法alertView:clickedButtonAtIndexを実装.hファイルでUIAlertViewStylePlainTextInput

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" 
               message:@"Message" 
               delegate:self 
             cancelButtonTitle:@"Done" 
             otherButtonTitles:nil]; 
alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
[alert show]; 

alertViewStyleプロパティを設定します。

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    NSLog(@"%@", [alertView textFieldAtIndex:0].text); 
} 
関連する問題