问题描述
|
我在视图上有一些UITextField,当点击它时会弹出键盘。有一个按钮也可以导航到另一个视图。当我在键盘处于活动状态时从此视图导航到第二视图时,会出现问题。当我从第二个视图导航回来时,键盘将独立出现。我该如何预防?
-(IBAction) loginButton:(id) sender
{
[currentTextField resignFirstResponder];
RequestPage *RequestPageview = [[RequestPage alloc] initWithNibName:nil bundle:nil];
[UIView beginAnimations:@\"flipping view\" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
forView:self.view.superview
cache:YES];
[UIView commitAnimations];
[self presentModalViewController:RequestPageview animated:YES];
//ß[self.view addSubview:RequestPageview.view];
}
//---when the keyboard appears---
-(void) keyboardDidShow:(NSNotification *) notification {
if (keyboardisShown) return;
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue *aValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect =
[self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view (with keyboard)---
CGRect viewFrame = [scrollview frame];
NSLog(@\"%f\",viewFrame.size.height);
viewFrame.size.height -= keyboardRect.size.height;
scrollview.frame = viewFrame;
NSLog(@\"%f\",keyboardRect.size.height);
NSLog(@\"%f\",viewFrame.size.height);
//---scroll to the current text field---
CGRect textFieldRect = [currentTextField frame];
[scrollview scrollRectToVisible:textFieldRect animated:YES];
keyboardisShown = YES;
NSLog(@\"Login Keyboard appear\");
}
//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue* aValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect =
[self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view back to the original size
// (without keyboard)---
CGRect viewFrame = [scrollview frame];
viewFrame.size.height += keyboardRect.size.height;
scrollview.frame = viewFrame;
keyboardisShown = NO;
NSLog(@\"Login Keyboard disappear\");
}
2011-05-27 16:57:20.628 LoginPage[322:207] Login view appear // loaded the app
2011-05-27 16:57:32.220 LoginPage[322:207] Login Keyboard appear // tap on textfield
2011-05-27 16:57:35.665 LoginPage[322:207] Request view appeared // navigate to second view with keyboard shown
2011-05-27 16:57:35.667 LoginPage[322:207] Login view disappear
2011-05-27 16:57:35.978 LoginPage[322:207] Request Keyboard disappear // weird? I should have hide the Login Keyboard instead
2011-05-27 16:57:39.738 LoginPage[322:207] Login view appear // navigate back
2011-05-27 16:57:39.740 LoginPage[322:207] Request view disappeared
解决方法
如果您想在单击按钮时隐藏键盘,并且有多个文本字段,则可以使用此代码...
[self.view endEditing:YES];
点按查看中的任何位置,键盘将消失。
请享用 !!...
,在loginButton方法中,在要编辑的文本字段上调用resignFirstResponder
。当用户导航回到该视图控制器时,键盘将不再存在。您需要稍微更改代码,以便对文本字段进行引用。如果只有一个文本字段,则IBOutlet将起作用。对于多个字段,请以视图控制器为委托,并在用户开始编辑时保留对当前文本字段的引用。您也可以遍历视图的子视图,尽管这样做可能效率不高。