0

ビューのどこでもタップでキーボードを閉じようとしています。これが私のコードです

- (void)registerForNotifcationOfKeyboard
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect bkgndRect = activeField.superview.frame;
    bkgndRect.size.height += kbSize.height;
    [activeField.superview setFrame:bkgndRect];
    [scrollView setContentOffset:CGPointMake(0.0,kbSize.height/2 - activeField.frame.origin.y) animated:YES];
}

- (void) textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

- (void) textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}

-(BOOL) disablesAutomaticKeyboardDismissal
{
    return NO;
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

}


- (void)viewDidLoad
{
    [self registerForNotifcationOfKeyboard];
     self.progressBar.hidden = YES;

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


    [super viewDidLoad];
}

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

どこでもタップを押すと、この関数dismissKeyboardが呼び出されますが、キーボードを閉じることはありません。

どなたか心当たりのある方はよろしくお願いします

よろしくお願いします

4

4 に答える 4

3

これは常に私にとってはうまくいきます:

//if `dismissKeyboard` is located in your `UIViewController`'s sublass: 
-(void) dismissKeyboard
{
   [self.view endEditing:YES];
}

UIView クラス リファレンスから:

 This method looks at the current view and its subview hierarchy for the text field that is currently the first responder.   
If it finds one, it asks that text field to resign as first responder. If the force parameter is set to **YES**, the text field is never even asked; it is **forced to resign**.
于 2012-09-21T06:17:36.260 に答える
1

@Lukaszに完全に同意します:ビューのendEditingプロパティを使用してキーボードを非表示にします

-(void) dismissKeyboard
{
  [self.view endEditing:YES]; //this will dissmiss keyboard;
}
于 2012-09-21T06:03:43.123 に答える
0

ビュー内の任意の場所を押してキーボードを閉じたい場合は、次の2つの方法があります。

1:-[UIColor clearColor]を持ち、ビューのサイズと等しいUIButtonを用意し、ボタンのIBActionでキーボードを閉じます。これはうまくいくように見えますが、良い習慣ではありません。

2:-IDインスペクターに移動し、ビューのクラスをUIControlクラスに変更してから、キーボードを閉じるIBActionをクラスに追加します。

これがお役に立てば幸いです。乾杯!!

于 2012-09-21T06:36:06.073 に答える
0

以下のコードを試してください。

その前に、.h ファイルで UITextFieldDelegate を使用します。

.m ファイルで

 textfieldname.delegate=self;



 - (BOOL)textFieldShouldReturn:(UITextField *)textField
{


    [textfieldname resignFirstResponder];

    return YES;

}

上記のコードは、return キーを押すとキーボードを閉じます。関数内の以下のコードを使用して、キーボードを閉じます。

[textfieldname resignFirstResponder];
于 2012-09-21T06:05:39.737 に答える