我试图显示从textfield
uialertview
获得的一些文字,而不是我想将文字显示为UILabel
。
我发现很难,有人可以帮帮我吗?
- (IBAction)buttonPressed1:(id)sender {
//http://useyourloaf.com/blog/uialertcontroller-changes-in-ios-8/
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Hello Crazy" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.placeholder = NSLocalizedString(@"Pet Name", @"Name");
}];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alert dismissViewControllerAnimated:YES completion:nil];
// self.labelText1.text = [NSString stringWithFormat:@"alert.textFields.firstObject"];
// self.labelText1.text = [NSMutableString stringWithString:@"alert.textFields.firstObject"];
self.labelText1.text = @" \?alert.textFields.firstObject\? ";
}];
[alert addAction:ok];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
在这里,您可以看到我正在尝试在self.labelText1.text
中显示文字,此处此labeltext1接受一个字符串,但我想显示从placeholder
uialertview
获取的文字。请给我一些建议......
答案 0 :(得分:1)
您可以使用alert.textFields
来获取文本字段,它是UITextField
的数组。在您的情况下,该数组的第一个对象包含您要使用的文本字段:
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
UITextField *textField = alert.textFields.firstObject;
self.labelText1.text = textField.text;
// Do other stuffs
}];
答案 1 :(得分:-1)
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[alert dismissViewControllerAnimated:YES completion:nil];
UITextfiled *tf = alert.textFields.firstObject;
self.labelText1.text = tf.text;
}];