我正在尝试从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];
}
}发布于 2014-12-31 03:11:07
这是“干净复制/粘贴”的版本:
Swift 3+:
let alert = UIAlertController(title: "Alert Title",
message: "Alert message",
preferredStyle: .alert)
let ok = UIAlertAction(title: "OK",
style: .default) { (action: UIAlertAction) in
if let text = alert.textFields?.first?.text {
print("And the text is... \(text)")
}
}
let cancel = UIAlertAction(title: "Cancel",
style: .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)目标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实例应该在调用textField之后将textFields保存在它的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.
}如果由于某种原因不是,请提供更多的信息(因为这个问题仍然受到关注)。它似乎(通过一些评论),一些人对此有一些问题,但没有提供信息,除了‘它不工作’。
原始答案:
您的代码看起来很好,应该可以工作。
另一种方法是在UIAlertController *alert=...之前定义UITextField
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;-- 编辑
原始poster的代码现在可以像一样工作(在Xcode7,iOS 9下测试)。可能是以前版本中的错误。可能是因为在以前的版本中,textField是由弱引用持有的,除非另一个强指针持有它,否则会被释放。
https://stackoverflow.com/questions/27693142
复制相似问题