这里的 '干净的复制/粘贴' 版本:
斯威夫特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
定义UITextField
前UIAlertController *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
被一个弱引用所控制,并被释放,除非另一个强指针持有它。
尝试在同一个块中插入'NSLog(@“alert.textFields:%@”,alert.textFields);''。 – bauerMusic
好的,我做了NSLog并得到了这个:alert。textFields:( “<_UIAlertControllerTextField:0x7b941230; frame =(4 4; 229.333 16); text ='yyyy'; clipsToBounds = YES; opaque = NO; gestureRecognizers =; layer = > “ ) 请参阅我输入的text =”yyyy“。那么如何让文本进入我的字符串? –