Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 无法在点击时关闭键盘_Iphone_Objective C_Cocoa Touch_Ios5 - Fatal编程技术网

Iphone 无法在点击时关闭键盘

Iphone 无法在点击时关闭键盘,iphone,objective-c,cocoa-touch,ios5,Iphone,Objective C,Cocoa Touch,Ios5,我试图在视图中的任何位置取消点击键盘。这是我的密码 - (void)registerForNotifcationOfKeyboard { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:)

我试图在视图中的任何位置取消点击键盘。这是我的密码

- (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];
}
当我在任何地方按tap键时,会调用此函数
解除键盘
,但它从不解除键盘

[textfieldname resignFirstResponder];
如果有人有任何想法,请告诉我


致以最诚挚的问候

我完全同意@Lukasz的观点:使用view的endEditing属性解除键盘锁定

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

我完全同意@Lukasz的观点:使用视图的endEditing属性来解除键盘的锁定

-(void) dismissKeyboard
{
  [self.view endEditing:YES]; //this will dissmiss keyboard;
}
请尝试下面的代码

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

在.m文件中

 textfieldname.delegate=self;



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


    [textfieldname resignFirstResponder];

    return YES;

}
按回车键时,上述代码将关闭键盘。在函数中使用以下代码可关闭键盘

[textfieldname resignFirstResponder];
请尝试下面的代码

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

在.m文件中

 textfieldname.delegate=self;



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


    [textfieldname resignFirstResponder];

    return YES;

}
按回车键时,上述代码将关闭键盘。在函数中使用以下代码可关闭键盘

[textfieldname resignFirstResponder];

这个总是对我有用:

//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**.

这个总是对我有用:

//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:-有一个UIButton,它具有[UIColor clearColor],并且等于视图的大小,在按钮的iButton中,您可以关闭键盘。这不是一个好的做法,尽管它似乎有效

2:-转到identity inspector并将视图的类更改为UIControl类,然后将iAction添加到类中,该类将关闭键盘

[textfieldname resignFirstResponder];
我希望这有帮助。
干杯

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

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

2:-转到identity inspector并将视图的类更改为UIControl类,然后将iAction添加到类中,该类将关闭键盘

[textfieldname resignFirstResponder];
我希望这有帮助。
干杯

他想用他的方法解除他的键盘
解除键盘
而不是在点击回车键时。-(无效)解除键盘{[textfieldname resignFirstResponder];}请查看他的代码的最后几行,他已经在这样做了。我猜他可能没有将代理连接到.xib文件中的文件所有者。他已经为
uitappesturerecognizer
编写了代码,将代理设置为
UITextField
无关紧要。他想用他的方法
dismissboard
关闭键盘,而不是在点击return时。-(无效)dismissKeyboard{[textfieldname resignFirstResponder];}请查看他的代码的最后几行,他已经在这样做了。我猜他可能没有将代理连接到.xib文件中的文件所有者。他已经为
UITapgestureReconfizer
编写了该代码,将委托设置为
UITextField
无关紧要。您是否已将委托正确连接到xib文件中的文件所有者?或者您可以使用activeField。委托=self;在viewDidLoad.com中,您是否正确地将委托连接到xib文件中的文件所有者?或者您可以使用activeField.delegate=self;在viewDidLoad中,@lukasz以这种方式关闭键盘是正确的,@lukasz以这种方式关闭键盘是正确的。