ios下隐藏虚拟键盘

网上收集以及自己实践运行的总结一下:

1:点击输入区以及退出输入区的时候,键盘自动消失

点击回车return,第一次从输入id区到密码区,第二次return 键盘消失

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField == userId){
        [userPass becomeFirstResponder]; //焦点转到密码区
    }else if(textField == userPass){
        [userPass resignFirstResponder]; //键盘消失
        
    }
       return YES;
}

2:点击其它区域,键盘消失。

补充touchesEnded触摸事件的介绍:http://www.cnblogs.com/spiritstudio/archive/2011/05/26/2059352.html

在输入区以外的地方,即此画面的背景View增加事件(手指在)

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [loginBlackBgView endEditing:NO];//loginBlackBgVIew背景View 
}

3:键盘显示时,点击Home键盘,程序退到后台,在次启动此程序时,不想键盘显示。

则在AppDelegate.m文件中,进入后台程序的函数中,增加隐藏函数。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self.window endEditing:YES];
  
}


4:同3转到后台时候,让键盘消失

来自:http://stackoverflow.com/questions/9307250/suspending-keyboard-when-user-press-on-home-button

在LoginViewController.m中修改内容

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
增加对应函数

- (void) receivedNotification:(NSNotification *) notification
{
    if ([userId isFirstResponder])
    {
        [userId resignFirstResponder];
    }
    else if ([userPass isFirstResponder])
    {
        [userPass resignFirstResponder];
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值