xcode 的新手,我正在 xcode 4.2 中创建一个简单的登录表单,我想隐藏键盘,我认为我有正确的代码,从教程中它说我需要将视图的类更改为 UIControl 但那里对此没有选择,在使用故事板时还有其他方法吗?
- (IBAction)backGroundTouched:(id)sender
{
[emailTextField resignFirstResponder];
[passTextField resignFirstResponder];
}
xcode 的新手,我正在 xcode 4.2 中创建一个简单的登录表单,我想隐藏键盘,我认为我有正确的代码,从教程中它说我需要将视图的类更改为 UIControl 但那里对此没有选择,在使用故事板时还有其他方法吗?
- (IBAction)backGroundTouched:(id)sender
{
[emailTextField resignFirstResponder];
[passTextField resignFirstResponder];
}
假设您在 viewCotroller 中执行它们,请调用
[self.view endEditing:YES];
如果您的两个文本字段是某些更高级别视图的子视图,您也可以使用[higherLevelView endEditing];
而不关心当前哪个子视图处于活动状态。
我遵循了本教程:http ://www.techotopia.com/index.php/Writing_iOS_7_Code_to_Hide_the_Keyboard它对我有用:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([_textField isFirstResponder] && [touch view] != _textField) {
[_textField resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
Make sure your both text fields is connect with it's IBOutlets.
No need to change UIView to UIControl.
// Connect every textfield's "Did end on exit" event with this method.
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
// Use this method also if you want to hide keyboard when user touch in background
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[emailTextField resignFirstResponder];
[passTextField resignFirstResponder];
}