标签:
关于iOS开发中键盘的退出,其实方法有很多中,而且我也学会了不少,包括各种非纯代码界面的退出。
其实这里纯代码界面推出如果用到Xib何Storyboard上面去还是一样的思路操作,只不过笔者在开发的时候是在纯代码界面遇到的问题,所以久以此命名。
下面大家介绍怎么在纯代码的情况下,退出(隐藏)键盘,Xib和StoryBoard情况下这里就不解释了(照此思路)。
关于UITextFiel个人感觉又很多中方法,但是最近开发中我用的最多的也就是这两种,根据和已经在公司上班的同行的聊天中,现在开发中用的最多的也就是这两种,当然如果你还想去学习其他的方法或者更牛逼的方法,亦或者你想在一般的程序员面前装一下逼那也是可以的。
1)点击Return
关于点击Return是要实现一个代理方法那就是
1 -(BOOL)textFieldShouldReturn:(UITextField *)textField
2 {
3
4
5 [textField resignFirstResponder];
6 return YES;
7 }
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.text endEditing:YES];
}
1 -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
2 {
3 if ([text isEqualToString:@"\n"]) {
4 [textView resignFirstResponder];
5 return NO;
6 }
7 return YES;
8 }
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.text endEditing:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.text endEditing:YES];
}
// 第一种是遵循代理,是在代理方法中实现键盘的隐藏
1 override func didReceiveMemoryWarning() {
2 super.didReceiveMemoryWarning()
3 // Dispose of any resources that can be recreated.
4 }
5 // 第一种方法。用代理实现键盘的隐藏
6 func textFieldShouldReturn(textField: UITextField) -> Bool {
7 if (textField == self.text) {
8
9 textField.resignFirstResponder()
10 // self.view.becomeFirstResponder()
11 }
12 return true;
13 }
14
// 第二种方法实现键盘的隐藏
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.text.resignFirstResponder()
self.pwdtext.resignFirstResponder()
}
//第1种收起键盘的方法
1 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
2 {
3 [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
4 }
//第2种收起键盘的方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}
标签:
原文地址:http://www.cnblogs.com/stronger-ios-lcx/p/5627812.html