切换到另一个视图时隐藏键盘

发布于 2024-11-09 14:54:42 字数 3048 浏览 0 评论 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 

I have some UITextField on the view, which will bring up the keyboard when it's tapped. There's a button which will navigate to another view also. Theres a problem when I navigate from this view to the 2nd view with a keyboard being active. When I navigate back from the 2nd view, the keyboard will appear on its own. How do I prevent this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我闹别瞎闹 2024-11-16 14:54:42

如果您想在点击按钮时隐藏键盘,并且您有多个文本字段,那么您可以使用此代码...

[self.view endEditing:YES];

点击视图中的任何位置,键盘将会消失...

享受吧!...

If you want to hide keyboard on tap of a button and if you have more than one text fields then you can use this code...

[self.view endEditing:YES];

Tap any where on view, and keyboard will dissappear...

Enjoy !!...

牵强ㄟ 2024-11-16 14:54:42

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

In the loginButton method, call resignFirstResponder on the text field that's being edited. When the user navigates back to that view controller the keyboard will no longer be present. You'll need to change your code slightly so you have a reference to the text field. If there is only one text field, an IBOutlet will do. For multiple fields, make your view controller the delegate and you can keep a reference to the current text field when the user begins editing. You can also traverse your view's subviews, although this is potentially not as efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文