以编程方式创建 UITextField 事件

发布于 2024-10-15 22:52:05 字数 407 浏览 0 评论 0原文

我在创建行时以编程方式将文本字段添加到 TableView 中。我试图通过执行此操作来订阅这些文本字段的 TouchUpInside 事件

UITextField *eTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 12, 145, 25)];
eTextField.delegate = self;
[eTextField addTarget:self action:@selector(showPicker)
forControlEvents:UIControlEventTouchUpInside];

showPicker 是一个没有参数的 IBAction。它永远不会被触发。还有什么我需要做的吗?

我有什么遗漏的吗? 提前致谢

I some add textfields programmatically into a TableView when the rows are created. I am trying to subscribe to the TouchUpInside event of these textFields by doing this

UITextField *eTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 12, 145, 25)];
eTextField.delegate = self;
[eTextField addTarget:self action:@selector(showPicker)
forControlEvents:UIControlEventTouchUpInside];

showPicker is an IBAction with no parameters.It never gets fired. Is there something I else I need to do?

Is there something I'm missing?
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦太阳 2024-10-22 22:52:05

不应该是 [eTextField addTarget:self action:@selector(showPicker*:*)
另外,您也没有分配 UIControlEvent。

所以:

[eTextField addTarget:self action:@selector(showPicker)

可能应该改为

[eTextField addTarget:self action:@selector(showPicker:) forControlEvents:UIControlEventEditingDidBegin];

Shouldn't it be [eTextField addTarget:self action:@selector(showPicker*:*)
Also you are not assign a UIControlEvent.

So:

[eTextField addTarget:self action:@selector(showPicker)

Should probably be changed in to

[eTextField addTarget:self action:@selector(showPicker:) forControlEvents:UIControlEventEditingDidBegin];
听风念你 2024-10-22 22:52:05

如果您尝试显示文本字段的选取器而不是键盘,则应将选取器选取器视图指定为文本字段的 inputView

但是,如果您想在用户触摸文本字段时触发某些方法,您应该覆盖
文本字段委托方法

textFieldDidBeginEditing

并调用 resign 第一响应者以避免键盘,然后编写您的自定义代码或方法调用。

If you are trying to show a picker for the text field instead of keyboard, you should assign the picker picker view as the inputView of the text field.

But if you want to fire some method when user touches the text field, you should override the
text field delegate method

textFieldDidBeginEditing

and call resign first responder to avoid the keyboard, and then write your custom code or method calls.

欢烬 2024-10-22 22:52:05

您可以尝试这个...

1.覆盖 viewController.m 文件中的 textFieldDidEndEditing
请参阅下面的示例代码:

-(BOOL)textFieldDidEndEditing:(UITextField*) tf{
 //your piece of code (goNext?)  
}

删除。

[eTextField addTarget:self action:@selector(goNext) forControlEvents:UIControlEventEventEditingDidEnd];

2.从代码中

You can try this...

1.Override textFieldDidEndEditing in your viewController.m file.
See the example code below:

-(BOOL)textFieldDidEndEditing:(UITextField*) tf{
 //your piece of code (goNext?)  
}

2.Remove

[eTextField addTarget:self action:@selector(goNext) forControlEvents:UIControlEventEventEditingDidEnd];

from your code.

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