3
votes

I have a UITextField named textFieldInput and some button. Somehow I disable the input view so that if anyone tap in the texField no keyboard will show. I am adding text when a button is pressed programmatically. And I want to catch this changes. I want to call a function when the text of textField will change. How Can I do that?

I tried by adding following function

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacementStr {
// some my code
return YES;
}

But this does not work. this only calls when i tap on the textField.

I also tried by adding following in my viewDidLoad function

[textFieldInput addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

this also doesn't work.

How can I do this?

Thanks.

2
means you want to you have a textfield which is not enable and when you press the button it should be enable .. right? - Jean-Luc Godard
@tzu.rahul no, i will add some text to that textfield when the button will be pressed. - razibdeb
okay than when button is pressed you are adding text , other than that it is disable ... right ? it has some texts but it is disable . it is only enable and editable only after button is pressesd .. am i sound correct? - Jean-Luc Godard
when button is pressed text will be added programmatically. and user can't enter text by taping on the textfield bye typing on keyboard. may be now you understand. - razibdeb
okay .. then why are you doing all this ? you should have that text whenever you are pressing the button right ? then why dont you just add it when anyone presses the button with the text which are there in the UITextField ? - Jean-Luc Godard

2 Answers

7
votes

Register notification.

 [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(textChanged:)
                                              name:UITextFieldTextDidChangeNotification
                                            object:YOUR_TEXT_FIELD];
1
votes
shouldChangeCharactersInRange:

this will work only when you set its delegate with the controller which implements this method. one other way is:

add these line viewDidLod

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    [nc addObserver:self
           selector:@selector (handle_TextFieldTextChanged:)
               UITextFieldTextDidChangeNotification
             object: textFieldInput];

and implement handle_TextFieldTextChanged:

- (void) handle_TextFieldTextChanged:(id)notification {

    // write your logic here.
}