Reputation: 5876
I want to get the current text field being edited when the keyboard was show. I need this to determine some y values for a frame animation. However, my keyboardWasShown method is running in the controller, so I can easily get the view but am uncertain how to get the correct text field. I have two text fields on this view.
//Move view to match keyboard when shown
-(void)keyboardWasShown:(NSNotification*)aNotification{
//Get frame of keyboard
NSValue* keyboardEndFrameValue = [[aNotification userInfo] objectForKey: UIKeyboardFrameEndUserInfoKey];
CGRect keyboardEndFrame = [keyboardEndFrameValue CGRectValue];
//Get animation properties of keyboard
NSNumber* animationDurationNumber = [[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration = [animationDurationNumber intValue];
NSNumber* animationCurveNumber = [[aNotification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey];
UIViewAnimationCurve animationCurve = [animationCurveNumber intValue];
UIViewAnimationOptions animationOptions = animationCurve << 16;
//Set up animation
[UIView animateWithDuration:animationDuration delay:0.0 options:animationOptions animations:^{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= keyboardEndFrame.origin.y;
//I need the access to the text field here to determine y value.
self.view.frame = viewFrame;
} completion:^(BOOL finished){}];
}
Upvotes: 2
Views: 2196
Reputation: 1201
use the Delegates of your UITextField and implement your
- (void)textFieldDidBeginEditing:(UITextField *)textField;
create an UITextField variable UITextField *flagTextField;
- (void)textFieldDidBeginEditing:(UITextField *)textField{
flagTextField = textField;
}
Now you have the instance of textfield
Upvotes: 1