如何将 uitextfield 中的文本显示到标签?
how can I show the text from uitextfield to a label?
我试图显示从 uialertview
的 textfield
中获得的一些文本,而不是我想将文本显示到 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 接受一个字符串,但我想显示从 uialertview
的 placeholder
中获得的文本=].请给我一些建议...
您可以使用 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
}];
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;
}];
我试图显示从 uialertview
的 textfield
中获得的一些文本,而不是我想将文本显示到 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 接受一个字符串,但我想显示从 uialertview
的 placeholder
中获得的文本=].请给我一些建议...
您可以使用 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
}];
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;
}];