Obj C - 辞去触摸 UIView 上的第一响应者

发布于 2024-11-01 05:33:40 字数 1085 浏览 3 评论 0原文

我试图让键盘在触摸屏幕时消失,这个问题在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲喜皆因你 2024-11-08 05:33:40

我发现以下代码在没有委托方法的情况下最适合我的文本视图(而不是文本字段):

首先在您的视图上设置一个点击手势识别器:

- (void)viewDidLoad{

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

}

然后在您的点击方法中:

- (void)tap:(id)sender
{
// use to make the view or any subview that is the first responder resign (optionally force)    
[[self view] endEditing:YES];
}

这应该允许您在以下情况下关闭键盘 :视图上的任何地方。

希望这有帮助

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 :

- (void)viewDidLoad{

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

}

and then in your tap method :

- (void)tap:(id)sender
{
// use to make the view or any subview that is the first responder resign (optionally force)    
[[self view] endEditing:YES];
}

this should allow your keyboard to be dismissed when you anywhere on the view.

Hope this helps

可可 2024-11-08 05:33:40

试试这个。我们很早就遇到了这个问题,但最终找到了正确的解决方案。

  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

        [yourtextfield resignFirstResponder];

        // you can have multiple textfields here


    }

这应该可以解决推动背景时键盘不消失的问题。

Try this. We had this problem eariler, but eventually found the right solution.

  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

        [yourtextfield resignFirstResponder];

        // you can have multiple textfields here


    }

This should resolve the problem with the keyboard not dissapearing when pushing the background.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文