繁体   English   中英

在iOS 5.0中使用Textfield显示AlertView时键盘无法打开

[英]Keyboard does not open when display alertview with Textfield in ios 5.0

我使用以下代码创建了带有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上运行的设备屏幕截图

在此处输入图片说明

我已经尝试过您的代码,并且它没有像您所说的那样在iOS 5中显示键盘,但是我只是在警报显示及其工作之后在您的textField中添加了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中重新resignFirstResponderresignFirstResponder不想将此代码放在代码末尾而不是resignFirstResponder代码而不是textField, resignFirstResponder对于低于6.0的iOS版本,只能使用resignFirstResponder

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

随着

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

你也写

alertTextField.delegate = self;

然后在您的头文件(即.h)中,传递文本字段委托,例如< UITextFieldDelegate >

尝试使用其他键盘UIKeyboardTypeDefault ,例如UIKeyboardTypeDefaultUIKeyboardTypePhonePad等。

也尝试[alertTextField setUserInteractionEnabled:YES];

alertTextField.delegate = self;

然后在委托方法中,

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

不太确定,但希望这对您有用。

试试这个,它在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];

希望这对您有帮助!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM