【问题标题】:how can I show the text from uitextfield to a label?如何将文本从 uitextfield 显示到标签?
【发布时间】:2016-06-27 06:36:42
【问题描述】:

我试图显示从uialertviewtextfield 获得的一些文本,而不是将文本显示到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 接受一个字符串,但我想显示从uialertviewplaceholder 获得的文本。请给我一些建议...

【问题讨论】:

    标签: ios objective-c uilabel uialertview


    【解决方案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
    }];
    

    【讨论】:

    • @downvoter:请说明您投反对票的原因,以便我改进。
    【解决方案2】:
    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;
    
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      相关资源
      最近更新 更多