我的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;
部分但仍然没有键盘出现。
(我在我的设备和模拟器上测试过它)
你对此有什么想法吗?
提前谢谢。
答案 0 :(得分:4)
我遇到了问题,因为我在视图中调用了UITextField委托方法textFieldDidBeginEditing时触发了警报,然后我将其填充了Alert视图文本字段的内容。这意味着我视野中的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];
}
答案 1 :(得分:1)
尝试以下代码:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Your name?"
message:nil
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];
适用于iOS5
答案 2 :(得分:0)
可以解决我的问题:
[enter resignFirstResponder];