2011-05-26 73 views
0

我在视图上有一些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 

回答

0

在loginButton方法调用,就会向正在编辑的文本字段resignFirstResponder。当用户导航回该视图控制器时,键盘将不再存在。您需要稍微更改代码,以便引用文本字段。如果只有一个文本字段,IBOutlet将执行。对于多个字段,将视图控制器设置为委托,并且可以在用户开始编辑时保留对当前文本字段的引用。你也可以遍历你的视图的子视图,尽管这可能没有那么高效。

+0

@March Charbonneau感谢您的答复。我设法让键盘在改变视图时隐藏,但有一些奇怪的行为。我在这两个页面上都有keyboardDidHide&keyboardDidShow方法,用于根据键盘的状态执行相应的操作。看起来,当键盘在第一个视图中处于活动状态时,如果我导航到第二个视图,它会在第二个视图中而不是第一个视图上调用KeyboardDidHide方法。同样,当键盘显示在第二个视图上并将其导航回视图1时,将调用第一个视图的keyboardDidHide而不是第二个视图。 – 2011-05-27 08:54:15

+0

我已经发布了代码和控制台日志,如果有帮助 – 2011-05-27 09:13:01

1

如果你想隐藏的一个按键,击键键盘,如果你有一个以上的文本字段,那么你可以使用此代码...

[self.view endEditing:YES]; 

点选查看任何地方,和键盘将dissappear ...

享受!! ...