2016-06-27 6 views
-1

textfielduialertviewであるテキストを表示しようとしていますが、テキストをUILabelに表示したいと思っています。uitextfieldのテキストをラベルに表示するにはどうすればよいですか?

私はそれが難しいと思っています、誰も私を助けてくれる?ここ

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

} 

あなたはこのlabeltext1は、文字列を受け入れ、ここで、私はself.labelText1.textでテキストを表示しようとしています見ることができますが、私はuialertviewplaceholderから得られたテキストを表示したいです。私にいくつかの提案をしてください...

答えて

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 
}]; 
+0

は私の人生Midhun MPを救った!!!!!ありがとうございます:) :) –

+1

@downvoter:私が改善できるように投票した理由を説明してください。 –

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

}]; 
関連する問題