3

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.

razibdeb
  • 1,211
  • 1
  • 10
  • 12
  • 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 Jul 25 '12 at 05:04
  • @tzu.rahul no, i will add some text to that textfield when the button will be pressed. – razibdeb Jul 25 '12 at 05:19
  • 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 Jul 25 '12 at 05:24
  • 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 Jul 25 '12 at 05:56
  • 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 Jul 25 '12 at 06:02

2 Answers2

7

Register notification.

 [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(textChanged:)
                                              name:UITextFieldTextDidChangeNotification
                                            object:YOUR_TEXT_FIELD];
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • i Think you need to give textFiled object for object: parameter in above. – Ishu Jul 25 '12 at 05:12
  • when i am typing you give the answer so +1 for you. – Ishu Jul 25 '12 at 05:13
  • thanks, but i add this code to viewDidLoad function and also create a function textChanged where i do something but when I am changing the text by clicking on the button this function did not called. Did I miss something? – razibdeb Jul 25 '12 at 05:22
1
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.
}
Ishu
  • 12,797
  • 5
  • 35
  • 51
  • Also, such kind of notifications should not be registered in viewDidLoad. Because once you push one more controller over it and edit text, it will call delegate method. Such notifications should be registered in viewWillAppear and should be unregistered in viewwillDisAppear. – Apurv Jul 25 '12 at 05:15
  • @Apurv I am going to register this in viewWillAppear and let you know weather it works or not. Thanks. – razibdeb Jul 25 '12 at 05:28
  • @Apurv I register that on my viewWillAppear method. But it didn't work. did i miss something? – razibdeb Jul 25 '12 at 05:58