我有一个视图,我在滚动视图中添加了文本字段。滚动是好的,但当我想在任何文本字段中写入文本后辞职键盘,那么我不能这样做!只要我点击除文本字段以外的任何位置,我就需要退出键盘。有人建议我写一堆代码:在点击任何部分视图时退出键盘
- (void)keyboardWillShow:(NSNotification *)n { // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown. This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField. If we were to resize the UIScrollView again, it would be disastrous. NOTE: The keyboard notification will fire even when the keyboard is already shown. if (keyboardIsShown) { return; } NSDictionary* userInfo = [n userInfo]; // get the size of the keyboard NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey]; CGSize keyboardSize = [boundsValue CGRectValue].size; // resize the noteView CGRect viewFrame = self.scroll.frame; // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView. viewFrame.size.height -= (keyboardSize.height - kTabBarHeight); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; // The kKeyboardAnimationDuration I am using is 0.3 [UIView setAnimationDuration:kKeyboardAnimationDuration]; [self.scroll setFrame:viewFrame]; [UIView commitAnimations]; keyboardIsShown = YES; } - (void)keyboardWillHide:(NSNotification *)n { NSDictionary* userInfo = [n userInfo]; // get the size of the keyboard NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey]; CGSize keyboardSize = [boundsValue CGRectValue].size; // resize the scrollview CGRect viewFrame = self.scroll.frame; /*I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.*/ viewFrame.size.height += (keyboardSize.height - kTabBarHeight); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; // The kKeyboardAnimationDuration I am using is 0.3 [UIView setAnimationDuration:kKeyboardAnimationDuration]; [self.scroll setFrame:viewFrame]; [UIView commitAnimations]; keyboardIsShown = NO; }
我想它不会给我我想要的。帮助PLZ
但我有这么多的文本字段在我看来。所以我怎么能设法在任何文本字段中写入文本后辞职键盘 – user720235 2011-05-16 05:51:06
@ user720235:这种情况下有一种方法,使用UITextField伊娃保存当前文本字段引用, – Jhaliya 2011-05-16 05:54:39