Search code examples
iosobjective-cuitextfielduitextfielddelegate

I want an event to trigger with the enter/return button and my UITextField


I have a UITextField and a button next to it. When the button is pressed, an event is triggered to test if what the user typed in the textfield is equal to what I'm looking for. I want to make it so that instead of having a button next to it they have to click, it triggers the event whenever they hit return/enter. I've been looking up how to do this for a while now and all I've found is stuff that doesn't work or is from like 2009-10. What should I do?

this is exactly what I have:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
      if ([UserText.text isEqualToString:@"hi"] || [UserText.text isEqualToString:@"hello"]) {
          test.hidden = false;
      }
      return NO;
  }

I also just added

UserText.delegate = self;

Solution

  • Why can't you use

        - (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
        // You can trigger your events here.
    
          return NO;
          }
    

    and also set delegate in storyboard. Whenever the user clicks on enter/return, this method will get called. You can trigger your event accordingly.