Why is my IBAction changing the label text not working?

I want to implement a label in an iOS app that changes its value to the textbox value when the user enters a new character into the textbox.

Hence, I wrote this IB action in my controller. listenerLabel

is an UILabel

IBOutlet and textInput

is an UITextField


- (IBAction)keyboardResponse:(id)sender
    listenerLabel.text = textInput.text;


Then I declared it in my header file.

- (IBAction)keyboardResponse:(id)sender;


Subsequently, in my xib file, I

  • dragged the line from the file owner into two UI elements and linked them to IBOutlets.
  • press Ctrl and click on the text field in the UIBuilder to link its "Value Changed" action to the IBAction keyboardResponse


However, when I run the app in the iOS Simulator, the label text does not change as I type letters into the text box. Why not?


source to share

2 answers

To do this, you need to use a notification. Assuming these outlets are properly connected to the label and text box, follow these steps:

in viewDidLoad:

[[NSNotificationCenter defaultCenter] 


Add this method (you don't need these IBActions

-(void)myTextChanged {
    self. listenerLabel.text = textInput.text;



As a side note - learn how to use the helper editor, it fixes errors when connecting IBActions or IBOutlets and automatically adds cleanup to viewDidUnload.



I would use the method UITextFieldDelegate

that was created for exactly this.

  • Make your view controller adopt this protocol. that is, in the .h file:

    @interface YourViewController : UIViewController <UITextFieldDelegate> {

  • In frontend Builder, set delegate

    of textInput

    to your view controller.

  • Then add the following to your .m file: (EDITED)

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        if (textField == textInput) {
            listenerLabel.text  = [textField.text stringByReplacingCharactersInRange:range withString:string];;
        // Note that if you don't want the characters to change in the textField 
        // you can return NO here:
        return YES;



All Articles