3

我对 UIAlertViewStylePlainTextInput 有疑问。我在文本字段中收到警报,但没有键盘出现..?这是我的代码:

UIAlertView *enter = [[UIAlertView alloc] initWithTitle:@"Highscores" message:@"Please Enter Your Name" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:@"Abbrechen" nil];

enter.alertViewStyle = UIAlertViewStylePlainTextInput;


UITextField *theTextField =  [enter textFieldAtIndex:0];
theTextField.placeholder = @"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;


[enter show];

我试过没有

UITextField *theTextField =  [enter textFieldAtIndex:0];
theTextField.placeholder = @"Your Name";
theTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
theTextField.selected = NO;

部分,但仍然没有键盘出现。

(我已经在我的设备和模拟器上测试过)

你对此有什么想法吗?

先感谢您。

4

3 回答 3

4

我遇到了问题,因为在我的视图中调用 UITextField 委托方法 textFieldDidBeginEditing 时触发了警报,然后我将使用警报视图文本字段的内容填充它。这意味着我认为 UITextField 成为第一响应者,我找不到辞职的方法。因此,如果您遇到问题,那是因为在显示 UIAlert 之前,有其他东西正在成为第一响应者。想想你是如何触发警报的。

我放弃在我的 UITextField 上使用轻敲手势,然后使用 UIAlertViewStylePlainTextInput 触发 UIAlertView 并且它工作正常。

下面的一些代码:

-(void)addTextFieldToView{
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 31)];
    textField.backgroundColor = [UIColor whiteColor];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.textAlignment = UITextAlignmentCenter;
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showAlertWithTextField:)];
    [textField addGestureRecognizer:tapGestureRecognizer];
    [self.view addSubview:textField];
}

-(void)showAlertWithTextField:(UITapGestureRecognizer *)gesture{
    UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil]; 
    [dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [dialog show]; 
}
于 2012-09-19T16:28:43.393 回答
1

试试下面的代码:

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Your name?"
                                                  message:nil
                                                 delegate:nil 
                                        cancelButtonTitle:@"Cancel" 
                                        otherButtonTitles:@"OK", nil];

[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];

适用于 iOS5

于 2012-03-02T17:11:54.977 回答
0

可能会解决您的问题,因为它解决了我的问题:

[enter resignFirstResponder];
于 2012-09-20T03:11:20.657 回答