2016-07-21 3 views
3

Как отобразить поле ввода текста в оповещении, чтобы получить вход от пользователя и использовать этот ввод обратно в приложении (отобразить ввод на метке)?Отображение предупреждения с вводом текстового поля

Как показывает ниже enter image description here enter image description here

Спасибо за ваше время.

ответ

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

Чтобы добавить TextField в UIAlertView установить alertViewStyle свойство с UIAlertViewStylePlainTextInput

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

В файле .h добавить UIAlertViewDelegate в качестве протокола и реализовать метод делегата alertView:clickedButtonAtIndex в файле .m.

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    NSLog(@"%@", [alertView textFieldAtIndex:0].text); 
} 
Смежные вопросы