2014-12-29 46 views
5

我想從UIAlertController文本字段中獲取文本。有人能告訴我我做錯了什麼,因爲它不工作。我得到一個NIL回報。UIAlertController獲取文本

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

嘗試在同一個塊中插入'NSLog(@「alert.textFields:%@」,alert.textFields);''。 – bauerMusic

+0

好的,我做了NSLog並得到了這個:alert。textFields:( 「<_UIAlertControllerTextField:0x7b941230; frame =(4 4; 229.333 16); text ='yyyy'; clipsToBounds = YES; opaque = NO; gestureRecognizers = ; layer = > 「 ) 請參閱我輸入的text =」yyyy「。那麼如何讓文本進入我的字符串? –

回答

28

這裏的 '乾淨的複製/粘貼' 版本:

斯威夫特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) 

斯威夫特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) 

目標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]; 

以前的答案:

編輯: - 請注意:目前,原來的問題似乎是工作原樣。

澄清:調用addTextFieldWithConfigurationHandler

UIAlertController實例應該持有它的文本框數組文本字段。

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. 
} 

如果由於某種原因不是,請說明更多的信息(因爲這個問題仍然受到關注)。 (通過一些評論)似乎有些人對此有一些問題,但沒有提供超出「無效」的信息。


原來的答覆:

您的代碼看起來不錯,它應該工作。

另一種方法是從addTextFieldWithConfigurationHandler定義UITextFieldUIAlertController *alert=...

UITextField *myTf;

傳遞文本字段:

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

      myTf = textField; 
     }]; 

然後在:

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; 

- 編輯

原來的海報代碼現在的作品是(在Xcode 7,iOS 9下測試)。可能是以前版本中的一個錯誤。可能是在以前的版本中,textField被一個弱引用所控制,並被釋放,除非另一個強指針持有它。

+0

工作。仍然不確定爲什麼它沒有以其他方式工作。謝謝您的幫助!! –

+1

它不會以您的方式工作,因爲您在將此警報顯示在屏幕後會失去對「警報」的引用。你應該在你的ViewController的全局變量中保留一個引用。 –

+1

@HoaParis首先,它**工作**。嘗試一個乾淨的項目。目前它的工作原理和OP一樣(在以前的版本中有錯誤?)。其次,有關塊的有趣的事情,他們捕捉變量。 – bauerMusic