2014-12-29 17 views
5

Tôi đang cố gắng lấy văn bản từ trường văn bản UIAlertController. Ai đó có thể cho tôi biết những gì tôi đã làm sai vì nó không hoạt động. Tôi nhận được tiền NIL.UIAlertController nhận văn bản

- (IBAction)btnMakeRec { 

     UIAlertController *alert= [UIAlertController 
            alertControllerWithTitle:@"Recipe Name?" 
            message:@"" 
            preferredStyle:UIAlertControllerStyleAlert]; 

     UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
                handler:^(UIAlertAction * action){ 


               UITextField *temp = alert.textFields.firstObject; 

               RecipeDesc.text = temp.text; 
               // HERE temp is Nil 


               RDescription = [[NSString alloc ] initWithFormat:@"%@", RecipeDesc.text]; 


                }]; 

     UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action) { 

                 // NSLog(@"cancel btn"); 

                  [alert dismissViewControllerAnimated:YES completion:nil]; 

                 }]; 

     [alert addAction:ok]; 
     [alert addAction:cancel]; 

     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
      textField.placeholder = @"Enter the name of the recipe"; 
      textField.keyboardType = UIKeyboardTypeDefault; 
     }]; 

     [self presentViewController:alert animated:YES completion:nil]; 

    } 

} 
+0

Cố gắng chèn 'NSLog (@" alert.textFields:% @ ", alert.textFields);' vào cùng một khối. – bauerMusic

+0

OK Tôi đã làm NSLog và nhận được điều này: cảnh báo.textFields: ( "<_UIAlertControllerTextField: 0x7b941230; khung = (4 4; 229.333 16); văn bản = 'yyyy'; clipToBounds = YES; opaque = NO; gestureRecognizers = ; lớp = > " ) Xem văn bản =" yyyy "đó là những gì tôi đã nhập. Vì vậy, làm thế nào để có được văn bản đó vào chuỗi của tôi? –

Trả lời

28

Đây là 'sạch copy/paste' phiên bản:

Swift 3:

let alert = UIAlertController(title: "Alert Title", 
           message: "Alert message", 
           preferredStyle: UIAlertControllerStyle.alert) 

let ok = UIAlertAction(title: "OK", 
         style: UIAlertActionStyle.default) { (action: UIAlertAction) in 

         if let alertTextField = alert.textFields?.first, alertTextField.text != nil { 

          print("And the text is... \(alertTextField.text!)!") 

         } 


} 

let cancel = UIAlertAction(title: "Cancel", 
          style: UIAlertActionStyle.cancel, 
          handler: nil) 

alert.addTextField { (textField: UITextField) in 

    textField.placeholder = "Text here" 

} 

alert.addAction(ok) 
alert.addAction(cancel) 

self.present(alert, animated: true, completion: nil) 

Swift 2:

let alert = UIAlertController(title: "Alert Title", 
           message: "Alert message", 
           preferredStyle: UIAlertControllerStyle.Alert) 

let ok = UIAlertAction(title: "OK", 
         style: UIAlertActionStyle.Default) { (action: UIAlertAction) in 

         if let alertTextField = alert.textFields?.first where alertTextField.text != nil { 

          print("And the text is... \(alertTextField.text!)!") 

         } 


} 

let cancel = UIAlertAction(title: "Cancel", 
          style: UIAlertActionStyle.Cancel, 
          handler: nil) 

alert.addTextFieldWithConfigurationHandler { (textField: UITextField) in 

    textField.placeholder = "Text here" 

} 

alert.addAction(ok) 
alert.addAction(cancel) 

self.presentViewController(alert, animated: true, completion: nil) 

Objective C:

UIAlertController *alert = [UIAlertController 
          alertControllerWithTitle: @"Alert Title" 
          message: @"Alert message" 
          preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction *ok = [UIAlertAction actionWithTitle: @"OK" style: UIAlertActionStyleDefault 
              handler:^(UIAlertAction *action){ 


               UITextField *alertTextField = alert.textFields.firstObject; 

               NSLog(@"And the text is... %@!", alertTextField.text); 

              }]; 

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel 
               handler: nil]; 


[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 

    textField.placeholder = @"Text here"; 

}]; 

[alert addAction:ok]; 
[alert addAction:cancel]; 

[self presentViewController:alert animated:YES completion:nil]; 

câu trả lời trước:

Edit: - Xin lưu ý: Hiện nay, câu hỏi ban đầu dường như được làm việc như vậy.

Để làm rõ:

Các dụ UIAlertController nên giữ TextField trong mảng textfields của nó sau khi gọi addTextFieldWithConfigurationHandler.

UIAlertController *alertController = ...// Create alert 

// Assuming you called 'addTextFieldWithConfigurationHandler' on 'alertController' 

UIAlertAction *action = [UIAlertAction actionWithTitle: ... handler:^(UIAlertAction * action) { 

    // alertController.textFields should hold the alert's text fields. 
} 

Nếu vì lý do nào đó không, vui lòng giảm nhiều ánh sáng hơn (vì vấn đề này vẫn đang được chú ý). Dường như (theo một số ý kiến) rằng một số người có một số vấn đề với điều này, nhưng không cung cấp thông tin ngoài 'nó không hoạt động'.


câu trả lời gốc:

Mã của bạn có vẻ tốt đẹp, cần làm việc.

Một cách khác là xác định một UITextField trước UIAlertController *alert=...

UITextField *myTf;

Vượt qua TextField từ addTextFieldWithConfigurationHandler:

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
      textField.placeholder = @"Enter the name of the recipe"; 
      textField.keyboardType = UIKeyboardTypeDefault; 

      myTf = textField; 
     }]; 

Sau đó, trong:

UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction * action){... 
//UITextField *temp = alert.textFields.firstObject; 

// Get the text from your textField 
NSString *temp = myTf.text; 

- EDIT

Mã áp phích ban đầu hiện hoạt động như (được thử nghiệm dưới Xcode 7, iOS 9). Có thể đã là một lỗi trong phiên bản trước. Có thể là trong phiên bản trước, textField được tổ chức bởi một tham chiếu yếu và đã được phát hành trừ khi một con trỏ mạnh khác đang nắm giữ nó.

+0

hoạt động. vẫn không chắc tại sao nó không hoạt động theo cách khác. Cảm ơn đã giúp đỡ!! –

+1

Nó không hoạt động theo cách của bạn gây ra bạn mất tham chiếu đến 'cảnh báo' của bạn sau khi hiển thị cảnh báo này với màn hình. Bạn nên giữ một tham chiếu trong một biến toàn cục của ViewController của bạn. –

+1

@HoaParis Trước tiên, nó ** không hoạt động. Hãy thử nó trên một dự án sạch sẽ. Hiện tại nó hoạt động chính xác như OP đã làm nó (lỗi trong phiên bản trước?). Thứ hai, điều buồn cười về các khối, chúng nắm bắt các biến. – bauerMusic

Các vấn đề liên quan