在ios 5.0中使用Textfield显示alertview时,键盘无法打开

时间:2013-08-14 11:48:19

标签: iphone ios uialertview

我使用以下代码创建了带TextField的AlertView:

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"The person you selected does not have an email address on file. Please enter their email address below." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Cancel",@"Submit",nil];
alert.tag = 1;
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeEmailAddress;
alertTextField.placeholder = @"Enter Email address";
[alert show];

它在ios 6或后者中显示精细且开放的键盘,但在ios 5中不显示。

以下是我在ios 5.1上运行的设备截图

enter image description here

4 个答案:

答案 0 :(得分:1)

我已经尝试过您的代码,并且它没有像你说的那样在iOS 5中显示键盘,但我刚刚在alertfield中添加了resignFirstResponder并且它已经工作了。这是代码,

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"The person you selected does not have an email address on file. Please enter their email address below." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Cancel",@"Submit",nil];
alert.tag = 1;
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
// assert(alertTextField);
alertTextField.keyboardType = UIKeyboardTypeEmailAddress;
alertTextField.placeholder = @"Enter Email address";
[alert show];
[alertTextField resignFirstResponder];

如果您不希望在iOS 6及更高版本中使用resignFirstResponder而不是将此代码放在代码末尾而不是resignFirstResponder代码而不是textField仅用resignFirstResponder用于iOS版本小于6.0

float currentVersion = 6.0;     
if ([[[UIDevice currentDevice] systemVersion] floatValue] < currentVersion)
  {
     [alertTextField resignFirstResponder];
  }

答案 1 :(得分:0)

一起
    UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeEmailAddress;

你也写

alertTextField.delegate = self;

在您的标题文件(即.h)中,传递文本字段委托,例如&lt; UITextFieldDelegate&gt;

答案 2 :(得分:0)

尝试使用不同的键盘类型,例如UIKeyboardTypeDefaultUIKeyboardTypePhonePad

同时尝试[alertTextField setUserInteractionEnabled:YES];

alertTextField.delegate = self;

然后在委托方法中,

-(void)textFieldDidBeginEditing:(UITextField *)textField {     
    NSLog(@"textFieldDidBeginEditing method was called");
    if (!self.window.isKeyWindow) {
       [self.window makeKeyAndVisible];
    }
}

不太确定,但希望这对你有用..

答案 3 :(得分:0)

尝试这是在ios 5上运行

CGRect textFieldFrame = CGRectMake(15.0f, 105.0f, 255.0f, 31.0f);

UITextField *txtYourTextField = [[UITextField alloc] initWithFrame:textFieldFrame];

txtYourTextField.placeholder = @"Your String";
txtYourTextField.backgroundColor = [UIColor whiteColor];
txtYourTextField.textColor = [UIColor blackColor];
txtYourTextField.font = [UIFont systemFontOfSize:14.0f];
txtYourTextField.borderStyle = UITextBorderStyleRoundedRect;
txtYourTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
txtYourTextField.returnKeyType = UIReturnKeyDefault;
txtYourTextField.keyboardType = UIKeyboardTypeEmailAddress;
txtYourTextField.delegate=self;

txtYourTextField.contentVerticalAlignment =    UIControlContentVerticalAlignmentCenter;
txtYourTextField.tag = 1;
txtYourTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;

UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"The person you selected does not have an email address on file. Please enter their email address below.\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Send",nil];
alert.tag=1;

[alert addSubview:txtYourTextField];
[alert show];

希望这可以帮助你!!!