网上收集以及自己实践运行的总结一下:
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];
}
来自: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];
}
}