简体   繁体   English

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

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

I have created AlertView with TextField using this code: 我使用以下代码创建了带有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];

It display fine and open keyboard in ios 6 or latter but not in ios 5. 它可以在ios 6或更高版本中正常显示并打开键盘,而在ios 5中则不显示。

Here is my device screenshot which run on ios 5.1 这是我在iOS 5.1上运行的设备屏幕截图

在此处输入图片说明

I have tried your code and its does not show keyboard in iOS 5 as you said but i have just added resignFirstResponder in your textField after alert show and its worked. 我已经尝试过您的代码,并且它没有像您所说的那样在iOS 5中显示键盘,但是我只是在警报显示及其工作之后在您的textField中添加了resignFirstResponder Here is the code, 这是代码,

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];

And if you don't want to resignFirstResponder in iOS 6 and later than put this code at the end of your code instead of resignFirstResponder code than textField only resignFirstResponder for iOS version less than 6.0 而且,如果您不想在iOS 6中重新resignFirstResponderresignFirstResponder不想将此代码放在代码末尾而不是resignFirstResponder代码而不是textField, resignFirstResponder对于低于6.0的iOS版本,只能使用resignFirstResponder

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

Along with 随着

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

you also write 你也写

alertTextField.delegate = self;

And in your header file (ie .h), pass textfield delegate like < UITextFieldDelegate > 然后在您的头文件(即.h)中,传递文本字段委托,例如< UITextFieldDelegate >

try with different keyboardTypes like UIKeyboardTypeDefault , UIKeyboardTypePhonePad , etc. 尝试使用其他键盘UIKeyboardTypeDefault ,例如UIKeyboardTypeDefaultUIKeyboardTypePhonePad等。

Also try [alertTextField setUserInteractionEnabled:YES]; 也尝试[alertTextField setUserInteractionEnabled:YES];

alertTextField.delegate = self;

then in the delegate method, 然后在委托方法中,

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

not pretty sure, but hope this works for you.. 不太确定,但希望这对您有用。

try this it is run on ios 5 试试这个,它在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];

Hope this helps you!!! 希望这对您有帮助!!!

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

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