6

我正在为我的标准 TextField 使用“深色”风格的键盘。这是用于登录文本字段或“忘记我的密码”文本字段,用户在其中输入一些信息,提交它,如果成功,它们将被发送到另一个视图,通常由标准导航控制器 popViewControllerAanimated:。AlertView 可能会出现在两者之间。

我经常看到的问题是键盘打开,正常的“深”灰色,然后用户单击提交,可能会出现警报视图,并且在关闭时视图切换到下一个屏幕以前的键盘离开屏幕。在新屏幕上,另一个默认样式的键盘可能会或可能不会向上滑动然后消失(甚至没有集中在文本字段上!)。然后,当单击另一个文本字段或返回上一个视图并单击一个文本字段时,这个带有白键的黑色键盘会错误地出现。它继续出现在文本字段中,直到单击几下后某些东西能够将其恢复为正常的深灰色。

我试图在 popViewController 发生之前以各种方式关闭原始键盘,但它似乎没有帮助。如果 AlertView 出现在两者之间,我将 popViewController 绑定到单击 AlertView 按钮时的委托操作。键盘通常不会以足够快的速度消失,无法在按下之前离开。延迟无济于事。

编辑: alertview 似乎是这里的罪魁祸首,以某种方式干扰流行音乐和键盘。

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    [textfield resignFirstResponder];
    [self.view endEditing:YES];
    return YES;
}

-(IBAction)submitRequest {
    [textfield resignFirstResponder];
    [self.view endEditing:YES];

    // make API call, if call succeeds run this block {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"..."
                                                message:@"..."
                                               delegate:delegate
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil, nil];
          dispatch_async(dispatch_get_main_queue(), ^{
            [alert show];
          });         
    // }
}

// delegate after alert OK is pressed
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  [self.navigationController popViewControllerAnimated:YES];
}

我怎样才能避免这个黑/白键盘?

在此处输入图像描述

4

1 回答 1

3
Try using the below code. It works fine for iOS 8 and below version

    if (IS_OS_8_OR_LATER) {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *cancelAction = [UIAlertAction
                                       actionWithTitle:B_title
                                       style:UIAlertActionStyleCancel
                                       handler:^(UIAlertAction *action)
                                       {
                                           [self.navigationController popViewControllerAnimated:YES];

                                       }];
        [alertVC addAction:cancelAction];
        [self presentViewController:alertVC animated:YES completion:nil];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }
}
于 2016-05-27T13:30:59.320 回答