Programmatically create UITextField events

╄→尐↘猪︶ㄣ 提交于 2019-12-23 13:07:43

问题


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


回答1:


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.




回答2:


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];



回答3:


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.



来源:https://stackoverflow.com/questions/4917578/programmatically-create-uitextfield-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!