Last active
August 29, 2015 14:25
-
-
Save stdrc/cf8628436d0abcbb0ee2 to your computer and use it in GitHub Desktop.
UITextField 点击 Return 后隐藏键盘
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 首先把 UITextField 的 delegate 设置成当前的 ViewController, | |
// 然后在 ViewController.m 里添加下面一个代理方法: | |
- (BOOL)textFieldShouldReturn:(UITextField *)textField | |
{ | |
[textField resignFirstResponder]; | |
return YES; | |
} | |
// 如果有多个文本框需要输入, 希望点`Return`跳到下一个, 就给下一个文本框的 outlet 发送 becomeFirstResponder 消息即可. | |
// 如果要做类似于登录的东西, 要既可以点按钮登录, 也可以在最后一个文本框点 Return 登录的话, 可以加上下面这行代码: | |
if (textField == lastTextField) | |
[loginBtn sendActionsForControlEvents:UIControlEventTouchUpInside]; | |
// 这条消息模拟点击了 loginBtn. | |
// 需要注意的就是, 把多个 UITextField 的 delegate 设置到一个类的话, | |
// 它们是共用一套代理方法, 需要在代理方法里面去判断是哪个文本框. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment