我试图从UIAlertController文本字段中获取文本.有人可以告诉我,由于它不起作用,我做错了什么.我得到一个NIL回归.
- (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]; } }
bauerMusic.. 30
这是'干净的复制/粘贴'版本:
Swift 3&4:
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];
上一个答案:
编辑: - 请注意:目前,原始问题似乎是按原样运作.
澄清:
该UIAlertController
调用后实例应该持有它的文本框数组文本字段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.
}
如果由于某种原因它没有,请放弃更多的光(因为这个问题仍然受到关注).似乎(通过一些评论)有些人对此有一些问题,但没有提供超出"它不起作用"的信息.
原始答案:
你的代码看起来很好,它应该工作.
另一种方法是定义一个UITextField
之前UIAlertController *alert=...
UITextField *myTf;
从addTextFieldWithConfigurationHandler
以下位置传递textField :
[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
由弱引用持有并被释放,除非另一个强指针持有它.
这是'干净的复制/粘贴'版本:
Swift 3&4:
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];
上一个答案:
编辑: - 请注意:目前,原始问题似乎是按原样运作.
澄清:
该UIAlertController
调用后实例应该持有它的文本框数组文本字段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.
}
如果由于某种原因它没有,请放弃更多的光(因为这个问题仍然受到关注).似乎(通过一些评论)有些人对此有一些问题,但没有提供超出"它不起作用"的信息.
原始答案:
你的代码看起来很好,它应该工作.
另一种方法是定义一个UITextField
之前UIAlertController *alert=...
UITextField *myTf;
从addTextFieldWithConfigurationHandler
以下位置传递textField :
[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
由弱引用持有并被释放,除非另一个强指针持有它.