以编程方式调用textFieldShouldReturn [英] Programmatically call textFieldShouldReturn

查看:78
本文介绍了以编程方式调用textFieldShouldReturn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前项目在cocos2d v2下运行。

The current project runs under cocos2d v2.

我在CCLayer中添加了一个简单的UITextField。

I have a simple UITextField added to a CCLayer.

每当用户触摸textField时,就会出现一个键盘。

Whenever, the user touch the textField, a keyboard appears.

然后,当用户触摸返回按钮时,键盘就会消失并清除输入。

Then when the user touch the "return" button, the keyboard disappear and clears the input.

我试图做的是当用户触摸UITextField之外的任何地方时做同样的事情。

What I tried to do is to make the same thing when the user touches anywhere outside the UITextField.

我确实找到了一种方法,它的工作原理是:

I did find a method and it works :

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touch = [touches anyObject];
    if(touch.view.tag != kTAGTextField){
        [[[[CCDirector sharedDirector] view] viewWithTag:kTAGTextField] resignFirstResponder];
    }
}

但是,此方法未调用函数:

However, this method doesn't call the function :

- (BOOL)textFieldShouldReturn:(UITextField *)textField

我使用此功能进行一些计算并清除输入。因此,我希望ccTouchesBegan在文本字段为 resignFirstResponder时输入此textFieldShouldReturn。

I use this function to do some calculation and clear the input. Therefore, I would like ccTouchesBegan to enter this textFieldShouldReturn when the textfield is "resignFirstResponder".

推荐答案

来自 Apple文档


textFieldShouldReturn:
询问委托人文本字段是否应处理

textFieldShouldReturn: Asks the delegate if the text field should process the pressing of the return button.

因此仅在用户点击返回按钮时调用。

So it is only called when the user taps the return button.

我宁愿创建一个用于计算和清除输入的方法,并在需要调用该方法时调用它。示例:

I would rather create a method for the calculation and input clearing, and call that method whenever you want it to be called. Example:

- (void)calculateAndClearInput {
    // Do some calculations and clear the input.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // Call your calculation and clearing method.
    [self calculateAndClearInput];
    return YES;
}

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch* touch = [touches anyObject];
    if (touch.view.tag != kTAGTextField) {
        [[[[CCDirector sharedDirector] view] viewWithTag:kTAGTextField] resignFirstResponder];
        // Call it here as well.
        [self calculateAndClearInput];
    }
}

这篇关于以编程方式调用textFieldShouldReturn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆