I have this method in a class. How do I use it in my subclass (of this class) because when I call [self shiftViewUpForKeyboard]; it needs the argument but when I type theNotification, it gives an error. I know this is probably extremely basic, but it would really help me out a lot throughout the whole of my app.
- (void) shiftViewUpForKeyboard: (NSNotification*) theNotification;
{
CGRect keyboardFrame;
NSDictionary* userInfo = theNotification.userInfo;
keyboardSlideDuration = [[userInfo objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];
keyboardFrame = [[userInfo objectForKey: UIKeyboardFrameBeginUserInfoKey] CGRectValue];
UIInterfaceOrientation theStatusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if UIInterfaceOrientationIsLandscape(theStatusBarOrientation)
keyboardShiftAmount = keyboardFrame.size.width;
else
keyboardShiftAmount = keyboardFrame.size.height;
[UIView beginAnimations: @"ShiftUp" context: nil];
[UIView setAnimationDuration: keyboardSlideDuration];
self.view.center = CGPointMake( self.view.center.x, self.view.center.y - keyboardShiftAmount);
[UIView commitAnimations];
viewShiftedForKeyboard = TRUE;
}
Thank you kindly!
This looks like a notification handler. You should generally not call notification handlers on your own. Notification handler methods usually are called by a notification emitted by
NSNotificationCenter. The notification center sends anNSNotificationobject to the handler method. In your case, the notification includes some additional user info.You could resemble the user info dictionary in your code that should directly call the handler and pass that to the handler method (building up your own
NSNotificationobject with the required user info dictionary). However, that would be kind of error-prone and I would consider it a ‘hack’.I would recommend that you put your code into a distinct method, call that method from the notification handler from your question, and then use the distinct method for direct calls.
You would then have:
Implement the
doSomethingWithSlideDuration:frame:method as an instance method of your class. In the code where you call it directly, calldoSomethingWithSlideDuration:frameinstead of calling the notification handler.You need to pass the slide duration and frame on your own when calling the method directly.