无法解雇键盘

时间:2012-09-21 05:58:11

标签: iphone objective-c cocoa-touch ios5

我正试图在视图中的任何位置点按键盘。这是我的代码

- (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 个答案:

答案 0 :(得分: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**.

答案 1 :(得分:1)

我完全同意@Lukasz:使用view endEditing属性来关闭键盘

-(void) dismissKeyboard
{
  [self.view endEditing:YES]; //this will dissmiss keyboard;
}

答案 2 :(得分:0)

请尝试以下代码。

之前在.h文件中使用UITextFieldDelegate。

<。>文件中的

 textfieldname.delegate=self;



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


    [textfieldname resignFirstResponder];

    return YES;

}

上面的代码会在按回车键时关闭键盘。使用功能内部的代码来解除键盘。

[textfieldname resignFirstResponder];

答案 3 :(得分:0)

如果您想通过按视图中的任何位置来关闭键盘,那么您可以做两件事:

1: - 让UIButton具有[UIColor clearColor]并且等于视图的大小,并且在按钮的IBAction中你解除键盘。虽然它似乎有用,但这不是一个好习惯。

2: - 转到身份检查器并将您的视图类更改为UIControl类,然后将IBAction添加到您的类中,以解除您的键盘。

我希望这会有所帮助。 干杯!!