我有两个UITextFields
(例如用户名和密码),但按键盘上的return键时无法摆脱键盘。我该怎么做呢?
发布于 2011-06-01 00:04:03
首先,您需要在视图/视图控制器的头文件中遵循UITextFieldDelegate
协议,如下所示:
@interface YourViewController : UIViewController <UITextFieldDelegate>
然后在您的.m文件中,您需要实现以下UITextFieldDelegate
协议方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
[textField resignFirstResponder];
确保键盘被关闭。
确保在.m中初始化文本字段后,将视图/视图控制器设置为UITextField的委托:
yourTextField = [[UITextField alloc] initWithFrame:yourFrame];
//....
//....
//Setting the textField's properties
//....
//The next line is important!!
yourTextField.delegate = self; //self references the viewcontroller or view your textField is on
发布于 2011-06-01 00:03:27
如下所示实现UITextFieldDelegate方法:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
发布于 2011-06-01 00:06:35
有关此主题的完整讨论,请参阅Managing the Keyboard。
https://stackoverflow.com/questions/6190276
复制相似问题