Obj C - 在触摸UIView时辞职第一响应者

当触摸屏幕时,我试图让键盘消失,这个问题在stackoverflow上得到了解决。由于这里有一个线程,当按下回车键时,我能够让键盘消失。我没有运气背景触摸辞职第一响应者。正在输入该方法,我在方法中有一个NSLog说“在backgroundTouched中”,但键盘仍在那里。 我试过让UIView成为一个UIControl类,所以我可以使用触摸事件。 journalComment是一个UITextView。
-(IBAction)backgroundTouched:(id)sender
    {
        [journalComment resignFirstResponder];
        NSLog(@ "in backgroundTouched");

}
我也试过在backGroundTouched方法调用的所有东西下都有一个隐形按钮。我想也许我在界面构建器中遗漏了一些东西,但我不确定是什么。 感谢您的任何帮助! 这适用于完成按钮:
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
 replacementText:(NSString *)text
{
    // Any new character added is passed in as the "text" parameter
    if ([text isEqualToString:@"n"]) {
        // Be sure to test for equality using the "isEqualToString" message
        [textView resignFirstResponder];

        // Return FALSE so that the final 'n' character doesn't get added
        return FALSE;
    }
    // For any other character return TRUE so that the text gets added to the view
    return TRUE;
}
    
已邀请:
试试这个。我们有这个问题的预告片,但最终找到了正确的解决方案。
  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

        [yourtextfield resignFirstResponder];

        // you can have multiple textfields here


    }
这应解决键盘在按下背景时不会消失的问题。     
我发现以下代码最适合我的文本视图(不是文本字段)而没有委托方法: 首先,在视图上设置一个点击手势识别器:
- (void)viewDidLoad{

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

}
然后在你的tap方法中:
- (void)tap:(id)sender
{
// use to make the view or any subview that is the first responder resign (optionally force)    
[[self view] endEditing:YES];
}
这应该允许您在视图中的任何位置解除键盘。 希望这可以帮助     

要回复问题请先登录注册