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 上运行的设备屏幕截图

在此处输入图像描述

4

4 回答 4

1

我已经尝试过您的代码,但它没有像您所说的那样在 iOS 5 中显示键盘,但我刚刚resignFirstResponder在警报显示后添加到您的 textField 中并且它有效。这是代码,

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

而且,如果您不想resignFirstResponder在 iOS 6 及更高版本中将此代码放在代码末尾而resignFirstResponder不是 textField 仅resignFirstResponder适用于低于 6.0 的 iOS 版本

float currentVersion = 6.0;     
if ([[[UIDevice currentDevice] systemVersion] floatValue] < currentVersion)
  {
     [alertTextField resignFirstResponder];
  }
于 2013-08-15T05:30:10.643 回答
0

随着

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

你也写

alertTextField.delegate = self;

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

于 2013-08-14T11:49:19.660 回答
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];

希望这对你有帮助!!!

于 2013-08-14T14:05:36.123 回答
0

尝试使用不同的键盘类型,如UIKeyboardTypeDefault,UIKeyboardTypePhonePad等。

也试试[alertTextField setUserInteractionEnabled:YES];

alertTextField.delegate = self;

然后在委托方法中,

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

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

于 2013-08-14T13:06:34.957 回答