0

ビューにいくつかの UITextField があり、タップするとキーボードが表示されます。別のビューに移動するボタンもあります。キーボードがアクティブな状態で、このビューから 2 番目のビューに移動すると問題が発生します。2 番目のビューから戻ると、キーボードが単独で表示されます。これを防ぐにはどうすればよいですか?

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

2 に答える 2

1

ボタンのタップ時にキーボードを非表示にする場合、および複数のテキスト フィールドがある場合は、このコードを使用できます...

[self.view endEditing:YES];

ビュー上の任意の場所をタップすると、キーボードが消えます...

楽しみ !!...

于 2012-12-21T14:03:11.897 に答える
0

loginButton メソッドで、resignFirstResponder編集中のテキスト フィールドを呼び出します。ユーザーがそのView Controllerに戻ると、キーボードは表示されなくなります。テキスト フィールドを参照できるように、コードを少し変更する必要があります。テキスト フィールドが 1 つしかない場合は、IBOutlet で十分です。複数のフィールドの場合、View Controller をデリゲートにすると、ユーザーが編集を開始したときに現在のテキスト フィールドへの参照を保持できます。ビューのサブビューをトラバースすることもできますが、これは効率的ではない可能性があります。

于 2011-05-26T16:11:37.197 に答える