16

I got the code below from this SO question. I'm trying to slide up textfields when I begin editing (because they otherwise get covered by the iPhone keyboard). However, the log statement reveals that the textFieldDidBeginEditing method is not getting called.

I have the code below in two different subclasses of UIViewController. In one of them, for example, I have a textfield connected from the storyboard to the UIViewController like this

@property (strong, nonatomic) IBOutlet UITextField *mnemonicField;

I moved the textfield up to the top of the view (i.e. not covered by keyboard) so that I could edit it to try to trigger the log statement but it didn't work. The text field otherwise works as expected i.e. the data I enter is getting saved to coreData etc etc.

Can you explain what I might be doing wrong?

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"The did begin edit method was called");
    [self animateTextField: textField up: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField: textField up: NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    const int movementDistance = 180; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}
Community
  • 1
  • 1
Leahcim
  • 40,649
  • 59
  • 195
  • 334
  • Does any of the other delegate methods are called or when did you set the delegate? – Larme May 12 '14 at 14:03
  • @larme I have to set a delegate for this to work? didn't know that – Leahcim May 12 '14 at 14:03
  • `textFieldDidEndEditing`: is a delegate method. It's a `UITextFieldDelegate` method, not a `UITextField` one. – Larme May 12 '14 at 14:04
  • For me I had added my TextField to a UIViewController and that UIViewController's view I had added as a subview to my main VC. I needed to then go to my main VC and include the code `[self addChildViewController:myVC];` for textField delegates to start working in that VC. Amazed I haven't encountered this before – Albert Renshaw Jul 13 '18 at 02:17

4 Answers4

43

You have not assigned your delegate of UITextField in your ViewController class:

In your viewcontroller.m file, In ViewDidLoad method, do this:

self.mnemonicField.delegate=self; 

In your viewcontroller.h file, do this:

@interface YourViewController : ViewController<UITextFieldDelegate>
Khawar Ali
  • 3,462
  • 4
  • 27
  • 55
4

You should set up text field delegate to self. Add this line to viewDidLoad method:

self.mnemonicField.delegate = self;

and remember to add this line <UITextFieldDelegate> to conform to that protocol.

You can achieve the same effect in storyboard by control drag from desired UITextField to view controller and select delegate.

Greg
  • 25,317
  • 6
  • 53
  • 62
4

You created IBOutlet so just drag your textfield to viewController and set delegate

enter image description here Then in .h add the following

@interface ViewController : ViewController<UITextFieldDelegate>
Yohan
  • 1,108
  • 9
  • 21
1

In other case if you click any other button without moving the focus of Uitextfield then delegate will not be called, for that you need to explicitly call

yourtextfield.resignFirstResponder()
Pramod
  • 1,123
  • 2
  • 12
  • 33