我第一次使用UIAlertController
。我需要提示用户输入。无论如何,以下就是我所拥有的。
- (void)showAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Hello!" message:@"Type something.") preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *noButton = [UIAlertAction actionWithTitle:@"Cancel") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
UIAlertAction *yesButton = [UIAlertAction actionWithTitle:@"Enter") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//NSString *input = alert.textFields[0].text;
//NSLog(@"input was '%@'", input);
UITextField *textField = alert.textFields[0];
NSLog(@"%@",textField.text); // <====== It's not returned
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
[textField addTarget:self action:@selector(alertTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[yesButton setEnabled:NO];
}];
[alert addAction:noButton];
[alert addAction:yesButton];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)alertTextFieldDidChange:(UITextField *)sender {
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController) {
UITextField *textfield = alertController.textFields.firstObject;
UIAlertAction *okAction = alertController.actions.lastObject;
okAction.enabled = [self validateName2:textfield.text:5]; // function for validating user input
}
}
出于某种原因,当用户点击“是”时,应用程序不会返回用户输入。事实上,Xcode破坏了'NSString
* input = alert.textFields [0] .text中的'text'。奇怪的是UIAlertView
没有返回用户输入。这就是为什么我切换到UIAlertController
。
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 101) {
if (buttonIndex == 1) {
NSString *username = [[alertView textFieldAtIndex:0] text];
NSLog(@"%@",username); // <====== It's never returned.
}
}
}
无论如何,我想知道我对UIAlertController
做错了什么?
Muchos thankos
P.S。我正在使用Xcode 6.4(6E35b)。此问题可能是由Xcode引起的。如果我使用iPad2模拟器调试代码,它实际上将返回用户输入,而如果我使用iPhone4S模拟器则不会。
答案 0 :(得分:2)
将此用于alertcontroller中的textField
__block typeof(self) weakSelf = self;
UIAlertController *alertController;
alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.tag = 1001;
textField.delegate = weakSelf;
textField.placeholder = @"";
[textField addTarget:weakSelf action:@selector(alertTextFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
}];
//OK Button
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
UITextField *textField = alertController.textFields.firstObject;
// Do stuff here
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
- (void)alertTextFieldDidChange:(UITextField *)sender
{
alertController = (UIAlertController *)self.presentedViewController;
if (alertController)
{
UITextField *textField = alertController.textFields.firstObject;
UIAlertAction *okAction = alertController.actions.lastObject;
okAction.enabled = textField.text.length > 0; // condition to enable button
}
}
#pragma mark - UITextField delegate methods
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
答案 1 :(得分:1)
我建议使用UITextFieldDelegate
来处理textField!
如果textField.delegate = self
的长度为&gt;,请设置- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
,然后使用alertTextFieldDidChange
方法执行replacementString
中的操作0
您为UITextField提供了委托方法,因此您不必自己创建选择器,我建议您充分利用它们!
如果您想了解更多信息,请参阅以下文档! https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/