34

随着最新的 iOS 8.3 版本,我们的应用程序开始出现奇怪的行为。

完成文本字段编辑后,用户可以单击关闭按钮,该按钮会弹出一个UIAlertView. 当用户在alertview 中点击discard时,alertview 和当前视图都被关闭。但不知何故,在视图消失后键盘出现了,这对用户来说很烦人。

经过一些调试,似乎在关闭视图之前用户访问的最后一个文本字段显示了键盘。我在许多地方尝试了各种方法来endEditing获取当前视图(在显示之前UIAlertView,单击 中的按钮之后UIAlertView;我什至将焦点设置到视图的另一个 UI 元素)。它没有解决问题。

但是对于这个特定问题,我不确定这是一个常见问题还是我们需要解决的问题。在 iOS 8.3 之前一切正常。

我们知道这UIAlertView在 iOS 8 中已被弃用。我们开始迁移到UIAlertController. 但如果有任何解决方法,我们很乐意听到。

这是一些代码片段。

- (IBAction)closeTapped:(UIButton *)sender
{
    // try to resign first responder
    // [self.tfName resignFirstResponder];
    // [self.tfPosition resignFirstResponder];
    [self.view endEditing:YES];

    if(self.orderDetails.isOpen && self.orderItemChanged)
    {
        UIAlertView* saveAlert = [[UIAlertView alloc] initWithTitle:@"Unsaved Changes"
                                                            message:@"Your changes have not been saved. Discard changes?"
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Save", @"Discard", nil];
        [saveAlert show];
    }
    else
    {
        [self close];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(buttonIndex)
    {
        case 1: // Save
        {
            [self save];
            break;
        }
        case 2: // Discard
        {
            [self close];
            break;
        }
    }
}

- (void)close
{   
    [self.delegate dismissEditOrderItemVC];
}
4

7 回答 7

14

如果您的部署目标是 iOS 8+,请尝试UIAlertController.

这是一个快速修复UIAlertView:当您的文本字段或文本视图退出第一响应者时,延迟显示警报视图的调用。

[self performSelector:@selector(showAlertView) withObject:nil afterDelay:0.6];
于 2015-11-16T07:04:58.087 回答
3

如果有人对此感到困惑,我希望这会有所帮助:

if (NSClassFromString(@"UIAlertController")) {
    UIAlertController* alert = ...
}
else {
    UIAlertView* alert = ...
}
于 2015-06-04T12:17:41.120 回答
2

您需要更改 ios 8.3 的警报

首先把它放在你的视野中

#define IS_IOS8 [[UIDevice currentDevice].systemVersion floatValue] >= 8.0

然后

if (IS_IOS8) {

        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Unsaved Changes" message:@"Your changes have not been saved. Discard changes?" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *saveAction = [UIAlertAction
                                    actionWithTitle:@"Save"
                                    style:UIAlertActionStyleCancel
                                    handler:^(UIAlertAction *action)
                                    {
                                        [self save];
                                    }];

        UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:@"Cancel"
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action)
                                   {
                                       [alertVC dismissViewControllerAnimated:YES completion:nil];
                                   }];


        UIAlertAction *discardAction = [UIAlertAction
                                   actionWithTitle:@"Discard"
                                   style:UIAlertActionStyleCancel
                                   handler:^(UIAlertAction *action)
                                   {
                                       [alertVC dismissViewControllerAnimated:YES completion:nil];
                                   }];



        [alertVC addAction:saveAction];
        [alertVC addAction:cancelAction];
        [alertVC addAction:discardAction];
        [self.view.window.rootViewController presentViewController:alertVC animated:YES completion:nil];

这将对您有所帮助,因为它可以帮助我解决同样的问题。上面的代码与 ios 7 & 8 都兼容

于 2015-07-08T07:21:52.860 回答
2

我也是,在关闭 UIAlertController 后弹出一个键盘(光标在最后使用的 textView 中),这是一个非常简单的修复:

在构建和呈现 UIAlertController 之前,

使用 [_activeTextView resignFirstResponder]; 键盘将重新出现。使用 [self.view endEditing:YES]; 键盘不会再次出现。

我希望这可以帮助你。

于 2017-05-08T20:57:07.577 回答
1

如果文本字段是第一响应者,则在解除警报时它将自动弹出键盘。确保第一响应者被正确解雇:

[textField resignFirstResponder]

请记住:在表格或滚动视图中,有时该字段必须在屏幕上可见才能正确关闭响应者。

如果没有第一响应者处于活动状态,则在解除警报时不应出现键盘。

对于这个问题的特殊情况,我建议设置一个委托方法来监听“完成”按钮并在委托回调中退出第一响应者。

或者,在开始编辑时,您可以存储对当前活动文本字段的引用,然后在“clickedButtonAtIndex”方法中,如果活动文本字段仍然处于活动状态,您可以将其退出。

于 2015-05-08T18:15:53.513 回答
1

尝试使用下面的代码。它适用于 iOS 8 及以下版本

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

        UIAlertAction *cancelAction = [UIAlertAction
                                     actionWithTitle:@"OK"
                                     style:UIAlertActionStyleCancel
                                     handler:^(UIAlertAction *action)
                                     {

                                     }];
        [alertVC addAction:cancelAction];

        [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{

        }];
    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }

}

于 2015-08-19T16:51:42.240 回答
0

我注意到 textField 键盘和 alertViews 的一些奇怪行为......也许制作一个名为 disableKeyboard 的 bool 并像这样使用它:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    if (disableKeyBoard) {

        disableKeyboard = NO;
        return NO;

    } else {

        return YES;

    }

}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {

    disableKeyboard = YES;

}

这只是一种解决方法,并没有解决核心问题,无论它是什么。要使此方法起作用,您需要在标题中设置 alertView 和 textField 委托方法。

于 2015-05-08T18:09:56.303 回答