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

试试下面的代码。

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

在 .m 文件中

 textfieldname.delegate=self;



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


    [textfieldname resignFirstResponder];

    return YES;

}

上面的代码将在按下返回键时关闭键盘。在函数中使用下面的代码来关闭键盘。

[textfieldname resignFirstResponder];
于 2012-09-21T06:05:39.737 回答
0

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

1:- 有一个 UIButton 具有 [UIColor clearColor] 并且等于视图的大小,并且在您关闭键盘的按钮的 IBAction 中。这不是一个好的做法,尽管它似乎有效。

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

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

于 2012-09-21T06:36:06.073 回答