Ale*_*ove 22 objective-c uitextfield ios uitextfielddelegate
我在MyCustomUIView类中有一个UITextField,当UITextField失去焦点时,我想隐藏该字段并显示其他内容.
该代表UITextField
被设置为MyCustomUIView
通过IB,我也有'已结束退出'和'编辑已结束'事件指向其中的IBAction
方法MyCustomUIView
.
@interface MyCustomUIView : UIView {
IBOutlet UITextField *myTextField;
}
-(IBAction)textFieldLostFocus:(UITextField *)textField;
@end
Run Code Online (Sandbox Code Playgroud)
但是,当UITextField失去焦点时,这些事件似乎都没有被触发.你如何陷阱/寻找这个事件?
设置的代理UITextField
是MyCustomUIView
这样的,我收到textFieldShouldReturn
消息,完成后关闭键盘.
但我感兴趣的还在于确定用户何时按下屏幕上的其他区域(比如另一个控件或只是空白区域)并且文本字段失去焦点.
小智 28
尝试使用委托以获得以下方法:
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
NSLog(@"Lost Focus for content: %@", textField.text);
return YES;
}
Run Code Online (Sandbox Code Playgroud)
这对我有用.
Tim*_*wen 14
我相信您需要将您的视图指定为UITextField委托,如下所示:
@interface MyCustomUIView : UIView <UITextFieldDelegate> {
Run Code Online (Sandbox Code Playgroud)
作为一个额外的好处,这就是当你按下"完成"或返回按钮时键盘消失的方式,具体取决于你如何设置该属性:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
//This line dismisses the keyboard.
[theTextField resignFirstResponder];
//Your view manipulation here if you moved the view up due to the keyboard etc.
return YES;
}
Run Code Online (Sandbox Code Playgroud)