如何将uitextfield中的文本显示为标签?

时间:2016-06-27 06:36:43

标签: ios objective-c uilabel uialertview

我试图显示从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获取的文字。请给我一些建议......

2 个答案:

答案 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;

}];