DanielMed
DanielMed

Reputation: 43

For some reason the method DidBeginEditing: (UITextField *)textfield is not working

I'm trying to move the view up when the keyboard shows so it wont cover up the screen, but for some reason its the -(void)DidBeginEditing: (UITextField *)textfield is not working.

- (void)textFieldDidBeginEditing:(UITextField *)ga1
{
    /* should move views */
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 220);
}

- (void)textFieldDidEndEditing:(UITextField *)ga1
{
    /* should move views */
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y - 220);

}

its nor going into the method, can anyone tell me why?

Upvotes: 0

Views: 2474

Answers (3)

Smart guy
Smart guy

Reputation: 524

If implemented, it will get called in place of textFieldDidEndEditing

@interface MyViewController :UIVieController <UITextFieldDelegate>
@property UITextField *myTextField
@end

@implementation MyViewController{

}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.myTextfield.delegate=self;
}

    -(void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason{
            if(reason==UITextFieldDidEndEditingReasonCommitted)
            {
                NSLog(@"Committed");
            }
    }

Upvotes: 1

AdamG
AdamG

Reputation: 3718

In the interface of the class add the line , so in the .m file you would put above where it says @implementation...

@interface MyClassName () <UITextFieldDelegate>
 // properties can also go here
 // for example dragging the IBOutlet for the textfield from the storyboard
@end

You then in viewDidLoad should set the delegate of the UITextField like so...

-(void)viewDidLoad {
 // whatever code
 self.textField.delegate = self;
}

Alternatively, and more cleanly, you can do this in the story board by control clicking the text field and dragging the indicator to the view controller class icon (the icon to the furthest left) in the lower bar.

Also, why are you calling the argument to the textField in your implementation "ga1"? Best practice you should call it

 - (void)textFieldDidBeginEditing:(UITextField *)textField

One final note is that if you have multiple textFields you should set the delegate for each of them in the way described above. This is why the storyboard way of doing it is "cleaner," because it keeps you from having multiple delegate declarations in your code.

Upvotes: 5

Jake Lin
Jake Lin

Reputation: 11494

Implement <UITextFieldDelegate> Protocol for your class. And set the delegate to self. Here is Apple's document UITextFieldDelegate Protocol

Upvotes: 0

Related Questions