问题
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