Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I call a method as I click UITextField?

I want to ask that how can I call a method as I do click(touch for start writing) in iphone UITextField, as like we click UIButton and put method in "addTarget", is there any way for UITextFields ?

like image 334
Chatar Veer Suthar Avatar asked Dec 09 '25 01:12

Chatar Veer Suthar


2 Answers

implement a UITextFieldDelegate and do whatever you want to do in - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField or - (void)textFieldDidBeginEditing:(UITextField *)textField

You should use the first method if you want to stop the textfield from behaving like a textfield. For example if you want to open the textfield editor in a modal view. You can return NO there if you don't want this behavior.


Edit: Here is the code to call myMethod:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
    [self myMethod]; 
    return YES; 
}
like image 169
Matthias Bauch Avatar answered Dec 10 '25 17:12

Matthias Bauch


You can set textField's delegate and implement textFieldShouldBeginEditing: method in it - that method will get called when user taps the field and before it goes into editing.

See UITextFieldDelegate reference for more methods available.

like image 20
Vladimir Avatar answered Dec 10 '25 16:12

Vladimir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!