Đây là 'sạch copy/paste' phiên bản:
Swift 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)
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];
câu trả lời trước:
Edit: - Xin lưu ý: Hiện nay, câu hỏi ban đầu dường như được làm việc như vậy.
Để làm rõ:
Các dụ UIAlertController
nên giữ TextField trong mảng textfields của nó sau khi gọi 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.
}
Nếu vì lý do nào đó không, vui lòng giảm nhiều ánh sáng hơn (vì vấn đề này vẫn đang được chú ý). Dường như (theo một số ý kiến) rằng một số người có một số vấn đề với điều này, nhưng không cung cấp thông tin ngoài 'nó không hoạt động'.
câu trả lời gốc:
Mã của bạn có vẻ tốt đẹp, cần làm việc.
Một cách khác là xác định một UITextField
trước UIAlertController *alert=...
UITextField *myTf;
Vượt qua TextField từ addTextFieldWithConfigurationHandler
:
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Enter the name of the recipe";
textField.keyboardType = UIKeyboardTypeDefault;
myTf = textField;
}];
Sau đó, trong:
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;
- EDIT
Mã áp phích ban đầu hiện hoạt động như (được thử nghiệm dưới Xcode 7, iOS 9). Có thể đã là một lỗi trong phiên bản trước. Có thể là trong phiên bản trước, textField
được tổ chức bởi một tham chiếu yếu và đã được phát hành trừ khi một con trỏ mạnh khác đang nắm giữ nó.
Cố gắng chèn 'NSLog (@" alert.textFields:% @ ", alert.textFields);' vào cùng một khối. – bauerMusic
OK Tôi đã làm NSLog và nhận được điều này: cảnh báo.textFields: ( "<_UIAlertControllerTextField: 0x7b941230; khung = (4 4; 229.333 16); văn bản = 'yyyy'; clipToBounds = YES; opaque = NO; gestureRecognizers =; lớp = > " ) Xem văn bản =" yyyy "đó là những gì tôi đã nhập. Vì vậy, làm thế nào để có được văn bản đó vào chuỗi của tôi? –