gpt4 book ai didi

ios8 - UIAlertController 获取文本

转载 作者:行者123 更新时间:2023-12-04 16:17:12 27 4
gpt4 key购买 nike

我正在尝试从 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];

}

}

最佳答案

这是“干净的复制/粘贴”版本:
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)
objective-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 后,实例应该将 textField 保存在它的 textFields 数组中。 .
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被弱引用持有并被释放,除非另一个强指针持有它。

关于ios8 - UIAlertController 获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27693142/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com