2014-12-29 44 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