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;
}
I'm trying to get the keyboard to disappear when the screen is touched, a question that is answered all over stackoverflow. I was able to get the keyboard to disappear when the enter key was pressed thanks to a thread here. I'm not having luck on the background touch resigning the first responder. The method is being entered, I have an NSLog in the method saying, "in backgroundTouched" but the keyboard is still there.
I've tried making the UIView a UIControl class so I could use the touch event.
journalComment is a UITextView.
-(IBAction)backgroundTouched:(id)sender
{
[journalComment resignFirstResponder];
NSLog(@ "in backgroundTouched");
}
I've also tried having a invisible button under everything that calles the backGroundTouched method. I think it maybe that I'm missing something in interface builder, but I'm not sure what.
Thank you for any help!
This is what works for the done button:
-(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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现以下代码在没有委托方法的情况下最适合我的文本视图(而不是文本字段):
首先在您的视图上设置一个点击手势识别器:
然后在您的点击方法中:
这应该允许您在以下情况下关闭键盘 :视图上的任何地方。
希望这有帮助
I found the following code works best with my text view (not text field) without the delegate methods:
first you set up a tap gesture recognizer onto your view :
and then in your tap method :
this should allow your keyboard to be dismissed when you anywhere on the view.
Hope this helps
试试这个。我们很早就遇到了这个问题,但最终找到了正确的解决方案。
这应该可以解决推动背景时键盘不消失的问题。
Try this. We had this problem eariler, but eventually found the right solution.
This should resolve the problem with the keyboard not dissapearing when pushing the background.