Oto 'czysty copy/paste' wersja:
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)
Cel 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];
Poprzednia odpowiedź:
Edycja: - Uwaga: Obecnie, oryginalne pytanie wydaje się działać jak jest.
Dla wyjaśnienia:
Instancja UIAlertController
powinien trzymać textField w swojej tablicy pól tekstowych po wywołaniu 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.
}
Jeśli z jakiegoś powodu nie jest, proszę rzucić więcej światła (ponieważ ta kwestia wciąż przyciąga uwagę). Wydaje się (według niektórych komentarzy), że niektórzy ludzie mieli pewne problemy z tym, ale nie dostarczyli informacji poza "to nie działa".
Oryginalny odpowiedź:
Twój kod wygląda dobrze, to powinno działać.
Innym sposobem jest zdefiniowanie UITextField
przed UIAlertController *alert=...
UITextField *myTf;
pass textField z addTextFieldWithConfigurationHandler
:
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Enter the name of the recipe";
textField.keyboardType = UIKeyboardTypeDefault;
myTf = textField;
}];
Następnie, w:
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;
- EDYCJA
Oryginalny kod plakatu działa teraz tak, jak jest (testowany pod Xcode 7, iOS 9). Mogło być błędem w poprzedniej wersji. Możliwe, że w poprzedniej wersji textField
był trzymany przez słabe odniesienie i został zwolniony, chyba że trzymał go inny silny wskaźnik.
Spróbuj wstawić 'NSLog (@" alert.textFields:% @ ", alert.textFields);' w tym samym bloku. – bauerMusic
OK Zrobiłem NSLog i otrzymałem: alert.textFields: ( "<_UIAlertControllerTextField: 0x7b941230; frame = (4 4; 229.333 16); text =" yyyy "; clipsToBounds = YES; opaque = NO; gestRecognizers =; layer = > " ) Zobacz tekst =" rrrr ", to jest to, co wpisałem. Jak zdobyć ten tekst na mój ciąg? –