2016-06-27 5 views
-1

Je suis en train d'essayer de montrer du texte gagné à partir d'un textfield de uialertview, que je veux montrer le texte dans un UILabel.Comment afficher le texte de uitextfield sur une étiquette?

Je trouve cela difficile, quelqu'un pourrait-il m'aider?

- (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]; 

} 

ici vous pouvez voir que je suis en train de montrer le texte self.labelText1.text, ici cette labeltext1 accepte une chaîne, mais je veux montrer le texte gagné du placeholder du uialertview. s'il vous plaît donnez-moi une suggestion ...

Répondre

1

Vous pouvez obtenir le (s) champ (s) de texte en utilisant alert.textFields, c'est un tableau de UITextField. Dans votre cas, le premier objet de ce tableau contient le champ de texte que vous souhaitez utiliser:

UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ 
    UITextField *textField = alert.textFields.firstObject; 
    self.labelText1.text = textField.text; 
    // Do other stuffs 
}]; 
+0

sauvé ma vie Midhun MP !!!!! merci beaucoup :) :) –

+1

@downvoter: S'il vous plaît expliquer la raison pour laquelle vous avez voté, afin que je puisse améliorer. –

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

}];