显示一个带有文本框输入的警告提示

6
如何在警告框中显示文本输入字段以从用户获取输入并将该输入用于应用程序(在标签上显示输入)?
如下图所示: enter image description here enter image description here 感谢您的时间。
2个回答

17
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协议,并在.m文件中实现delegate方法alertView:clickedButtonAtIndex。
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%@", [alertView textFieldAtIndex:0].text);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接