2011-05-16 46 views
0

我有一個視圖,我在滾動視圖中添加了文本字段。滾動是好的,但當我想在任何文本字段中寫入文本後辭職鍵盤,那麼我不能這樣做!只要我點擊除文本字段以外的任何位置,我就需要退出鍵盤。有人建議我寫一堆代碼:在點擊任何部分視圖時退出鍵盤

 
- (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

回答

-1

您有2個選項

viewDidLoad

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];  
[self.view addGestureRecognizer:tap]; 

dismissKeyboard:功能

-(void)dismissKeyboard { 
     [aTextField resignFirstResponder]; 
} 

(其中aTextField是負責鍵盤文本字段)

OPT ION 2

如果你不能添加gestureRecognizer那麼你可以試試這個

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch * touch = [touches anyObject]; 
    if(touch.phase == UITouchPhaseBegan) { 
     [aTextField resignFirstResponder]; 
    } 
} 

所有幸得this answer

希望它有幫助。

1

您需要爲他人查看觸摸事件(除文本字段外),您可以通過執行方法UIResponder來實現。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch * touch = [touches anyObject]; 
    [myTextField resignFirstResponder]; 
} 
+0

但我有這麼多的文本字段在我看來。所以我怎麼能設法在任何文本字段中寫入文本後辭職鍵盤 – user720235 2011-05-16 05:51:06

+0

@ user720235:這種情況下有一種方法,使用UITextField伊娃保存當前文本字段引用, – Jhaliya 2011-05-16 05:54:39

0


試試下面的代碼

- (IBAction)backgroundTap:(id)sender 
{ 
    [yourtextfield resignFirstResponder]; 
} 

在XIB重視此方法的UIView觸及事件。

+0

但我有這麼多的文本字段在我的視圖。所以我怎麼能設法在任何文本字段中寫入文本後辭職鍵盤 – user720235 2011-05-16 05:51:35

+0

jst有一個文本字段的ivar,並將當前textfield的引用添加到textfieldShouldBeginEditing中。 – dks1725 2011-05-16 06:33:29

0

你可以做的一件簡單的事情是, 有一個覆蓋整個視圖並將該按鈕連接到@ dks1725提到的方法的UIButton。

0

我發現的最有效的(和最少的代碼)方法超過here。簡而言之,您可以簡單地將endEditing:TRUE發送到包含文本字段的超級視圖,並且所有的第一響應者狀態都由具有該字段的字段放棄。

相關問題