ios - 在 ios 8.3 及更高版本中,UIAlertView 导致 keyboardWillShow 和 keyboardWillHide 被调用两次

标签 ios objective-c xcode uialertview ios8.3

在做项目的时候,我遇到了这个问题。

其中一个 Controller 实现了keyboardWillShowkeyboardWillHide(来自 Apple 的标准代码 Managing the Keyboard)。 在后台点击时,UIAlertView 出现(基于一些验证),UIAlertView 中只有一个按钮可以简单地关闭 UIAlertView

问题出现在这里,在 UIAlertView 关闭时,再次调用了 keyboardWillShowkeyboardWillHide

下面是我遇到问题的代码,

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>
{
   int timeCalledShow;
   int timeCalledHide;
}
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
- (IBAction)backgroundTapped:(id)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {

    timeCalledShow+=1;
    NSLog(@"show Time Called %d", timeCalledShow);
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets;
    if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
        contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0);
} else {
        contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.width), 0.0);
}
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)keyboardWillHide:(NSNotification *)notification {
    timeCalledHide+=1;
    NSLog(@"Hide Time Called %d", timeCalledShow);
    self.scrollView.contentInset = UIEdgeInsetsZero;
    self.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)backgroundTapped:(id)sender {
    [[[UIAlertView alloc] initWithTitle:@"Testing" message:@"Keyboard hide & show, due to alert view" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
    [self.view endEditing:YES];
}
@end

注意事项

  1. 我已经检查了keyboardWillShow called twice和类似的问题在这里,但找不到答案
  2. 它适用于 iOS 7.0
  3. 这是Test Code的链接

编辑 我已经知道如何解决代码问题。但真正的问题是,UIAlertView 如何触发 keyboardWillShow 通知

编辑代码 我试过@Chonch也建议使用下面的代码,但使用此代码键盘甚至永远不会关闭。表示关闭 Alert 后键盘再次出现。

- (IBAction)backgroundTapped:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"testing" message:@"Keyboard" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {}];

    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
    [self.textField resignFirstResponder];
}

Question Posted at Apple Developer Forums

最佳答案

UIAlertView 有问题,可能自从它被弃用后就再也没有出现过。
UIAlertController 替换它,你的问题就会消失

关于ios - 在 ios 8.3 及更高版本中,UIAlertView 导致 keyboardWillShow 和 keyboardWillHide 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32265161/

相关文章:

ios - NSClassFromString() 总是返回 nil

ios - 查看 EkReminder 重复频率的最佳方式?

ios - iPad Pro 上的黑色状态栏带有圆角

ios - UIImageView 边界、框架

ios - 由于 "GCKConnectionSuspendReasonNetworkNotReachable",Cast session 暂停

ios - 优胜美地的 xcodeproj 中缺少图标文件

ios - 如何找出 NSString 中是否存在特定字符?

ios - Xcode 10 svnX 问题

iOS 改变一半图像的颜色

iOS UITableView - 向左滑动并单击单元格时显示删除按钮