Ios 使用UIAlertViewStylePlainTextInput显示UIAlertView,但不显示键盘

Ios 使用UIAlertViewStylePlainTextInput显示UIAlertView,但不显示键盘,ios,ios6,uialertview,uikeyboard,Ios,Ios6,Uialertview,Uikeyboard,因此,我希望将UIAlertView与alertViewStyle一起显示为UIAlertViewStylePlainTextInput,但我希望它在显示时不会显示与其关联的键盘。有人有主意吗 另外,要了解我希望它的外观,请继续并在模拟器中使用alertViewStyle=UIAlertViewStylePlainTextInput显示警报视图,然后按return。我希望UIAlertView集中在屏幕上。我刚刚找到了几种方法来实现这一点。第一种也是最肮脏的方法是立即致电辞职第一响应者。虽然很难

因此,我希望将
UIAlertView
alertViewStyle
一起显示为
UIAlertViewStylePlainTextInput
,但我希望它在显示时不会显示与其关联的键盘。有人有主意吗


另外,要了解我希望它的外观,请继续并在模拟器中使用
alertViewStyle=UIAlertViewStylePlainTextInput
显示警报视图,然后按return。我希望
UIAlertView
集中在屏幕上。

我刚刚找到了几种方法来实现这一点。第一种也是最肮脏的方法是立即致电
辞职第一响应者
。虽然很难看,但它很管用

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:@"Button", nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];
[[alert textFieldAtIndex:0] resignFirstResponder];
第二个选项是利用
UITextFieldDelegate
协议,特别是
textFieldShouldBeginEditing:
。使用此选项并将您的类设置为警报中文本字段的代理,您可以简单地返回
NO
,并阻止键盘显示

请务必记住,除非在此方法中提供某种条件,否则将无法编辑文本字段

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:@"Button", nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[[alert textFieldAtIndex:0] setDelegate:self];
[alert show];

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return NO;
}

这只是一个理论,还没有经过检验:-

  • 创建UIAlertView
  • 执行如下操作:-

    for(UIView *view in [alertView subViews])
    {
     if([view isKindOfClass:[UITextField class])
    {
     view.delegate=self
    }
    }
    
  • 实施该方法

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
       return NO;
    }
    
  • 显示警报视图

  • 在IOS6中工作,但在IOS7中不工作

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title"
                                                    message:@"")
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK", nil];
    
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    
    UITextField *textField = [alert textFieldAtIndex:0];
    textField.text = @"your text";
    [alert show];
    

    可以在IOS6和IOS7上工作

    为什么不想让键盘完全向上?我已经试过了,我注意到这样做不会使UIAlertView在模拟器的屏幕上居中。你也是这样吗?
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title"
                                                    message:@"")
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK", nil];
    
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    
    UITextField *textField = [alert textFieldAtIndex:0];
    textField.text = @"your text";
    [alert show];