簡體   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