关于UIAlertView键盘弹起的问题

这里记录一下我在开发中遇到的一个奇葩的问题,经过百度后我发现这个问题确实存在,也有不少人遇到过,今天就把它写出来。其实我们单纯的使用UIAlertView是没什么问题的,但是当UI中有输入框时,当输入完成后,我们点击按钮弹出UIAlertView的时候,在点击UIAlertView上面的按钮跳转到下一个页面的时候,那么问题就来了,在后一个页面会弹出键盘。这个问题刚开始出现的时候,莫名奇妙,也找了好久,因为在模拟器上是不容易察觉的,在真机上就非常明显了。

  • 下面我们模拟一下这个现象,在此来给大家加深印象,避免跟我一样遇到这个问题而纠结了。直接上代码。
  • #import "ViewController.h"
    #import "ViewControllerOne.h"
    
    @interface ViewController ()<UIAlertViewDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
        button.center = self.view.center;
        [self.view addSubview:button];
        [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 100, 30)];
        textField.backgroundColor = [UIColor redColor];
        [self.view addSubview:textField];
        [self obserKeyboard];
    }
    
    #pragma mark - 键盘的监控
    - (void)obserKeyboard {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardDidHideNotification object:nil];
    }
    
    - (void)click {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"测试UIAlertView" message:@"弊端" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"好玩么", nil];
        [alertView show];
    }
    #pragma mark 键盘动作
    - (void)keyboardWillShow:(NSNotification *)notif {
        NSLog(@"键盘将要弹出-keyboardWillShow");
    }
    - (void)keyboardShow:(NSNotification *)notif {
        NSLog(@"键盘已经弹出-keyboardShow");
    }
    - (void)keyboardWillHide:(NSNotification *)notif {
        NSLog(@"键盘将要隐藏-keyboardWillHide");
    }
    - (void)keyboardHide:(NSNotification *)notif {
        NSLog(@"键盘已经隐藏-keyboardWillHide");
        
    }
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == 1) {
            ViewControllerOne *ctl = [[ViewControllerOne alloc] init];
            [self.navigationController pushViewController:ctl animated:YES];
        }
    }

    简单说一下,就是在一个界面上有一个UITextField和一个按钮,按钮的点击事件就是弹出UIAlertView。下面说一下模拟现象的步骤。

  1. 首先在UITextField输入一下文字
  2. 输入完成后我们点击按钮,弹出UIAlertView这个时候我们监听到的键盘事件在控制台打印如下
  3. 当我们点击UIAlertView的确定按钮的时候,push到ViewControllerOne。其中ViewControllerOne的代码也就是键盘的监听,代码如下:
    #import "ViewControllerOne.h"
    
    @interface ViewControllerOne ()
    
    @end
    
    @implementation ViewControllerOne
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        [self obserKeyboard];
    }
    #pragma mark - 键盘的监控
    - (void)obserKeyboard {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardDidHideNotification object:nil];
    }
    #pragma mark 键盘动作
    - (void)keyboardWillShow:(NSNotification *)notif {
        NSLog(@"键盘将要弹出-keyboardWillShow");
    }
    - (void)keyboardShow:(NSNotification *)notif {
        NSLog(@"键盘已经弹出-keyboardShow");
    }
    - (void)keyboardWillHide:(NSNotification *)notif {
        NSLog(@"键盘将要隐藏-keyboardWillHide");
    }
    - (void)keyboardHide:(NSNotification *)notif {
        NSLog(@"键盘已经隐藏-keyboardWillHide");
        
    }

     当我们点击push进去的时候,我们再看控制台的打印信息。

  4. 在ViewControllerOne里面也监测到了键盘的事件。

这个会在真机上造成当push进入下一个界面的时候,屏幕上会出现一条黑线从右划到左边。其实是键盘弹起又立马收回去的现象造成的,当然解决办法我目前是用的UIAlertViewController来代替。还说一点,其实为了解决这个问题,我当初还在点击确定按钮的时候用[self.view endEditing:YES];来防止键盘弹出,但是实际是没有卵用,键盘还是会弹出来,后来我全部换成了UIAlertViewController。

posted @ 2016-08-01 11:25  降妖除魔来深圳  阅读(890)  评论(0编辑  收藏  举报