Programmatically create UITextField events

I add text fields programmatically in a TableView when rows are created. I am trying to subscribe to the TouchUpInside event of these text fields 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 starts. Is there anything else I need?

Is there something I am missing? thanks in advance

+4
source share
3 answers

If you are trying to display a picker for a text field instead of a keyboard, you must designate the picker picker inputView as the inputView text field.

But if you want to run any method when the user touches the text field, you must override the delegate method of the text field

textFieldDidBeginEditing

and cause the first responder to resign to escape the keyboard, and then write your own code or method calls.

+1
source

There should not be [eTextField addTarget: self action: @selector (showPicker *: *) Also you do not assign a UIControlEvent.

So:

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

Probably need to change to

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

You can try this ...

1. Return textFieldDidEndEditing to your viewController.m file. See sample code below:

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

2.Remove

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

from your code.

0
source

Source: https://habr.com/ru/post/1338669/


All Articles