首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在IOS8.3及更高版本中,UIAlertView导致keyboardWillShow & keyboardWillHide调用两次

在IOS8.3及更高版本中,UIAlertView导致keyboardWillShow & keyboardWillHide调用两次
EN

Stack Overflow用户
提问于 2015-08-28 07:03:38
回答 2查看 865关注 0票数 3

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

其中一个控制器实现了keyboardWillShow& keyboardWillHide (苹果管理键盘的标准代码)。在背景点击上,UIAlertView出现(根据一些验证),UIAlertView中只有一个按钮可以简单地关闭UIAlertView

问题出现在这里,在UIAlertView结束时,keyboardWillShowkeyboardWillHide再次调用。

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

代码语言:javascript
复制
#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

Notes

  1. 我已经在这里检查过keyboardWillShow打了两次电话和类似的问题,但是找不到答案。
  2. 它在iOS 7.0中运行得很好。
  3. 这是测试代码的链接

编辑我已经知道代码的工作了。但真正的问题是,a__UIAlertView如何触发a__keyboardWillShow通知?

编辑代码我已经尝试了下面的代码也是由@Chonch建议的,但是用这段代码键盘永远不会关闭。表示关闭警报后再次出现键盘。

代码语言:javascript
复制
- (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];
}

在苹果开发者论坛上发布的问题

EN

回答 2

Stack Overflow用户

发布于 2015-08-29 14:44:49

不知道为什么会这样。这可能与UIAlertView试图使键盘状态恢复到以前的状态有关。但是请注意,使用额外的显示/隐藏调用并没有造成伤害。一般说来,你需要为多个显示调用做好准备,因为它们也是在键盘样式改变时出现的。

如果您想摆脱它们,那么使用UIAlertController代替,就像Chonch已经建议的那样,并确保键盘在警报出现之前被取消,那么它就会正常工作:

代码语言:javascript
复制
- (IBAction)backgroundTapped:(id)sender {
    [self.textField resignFirstResponder];

    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];
}

请注意,使用UIAlertController时,还需要在视图控制器中保留对警报的引用,否则它将很快被释放。

票数 1
EN

Stack Overflow用户

发布于 2016-09-29 01:02:31

我刚解决了一个类似的问题。当警报解除后,键盘就会弹出,看上去像是苹果的窃听器。

有一个简单的解决方案:如果使用AlertController,只需将动画设置为NO即可。

代码语言:javascript
复制
[self presentViewController:alert animated:NO completion:nil];

如果它能解决你的问题,请告诉我

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32265161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档