I am having a hard time wording the title, and I also don't know if this question is good practice, but I am sure someone here will let me know.
I have a utility class and I am adding a class method that will hide the keyboard on a view controller.
In Utilities.m:
+ (void) hideKeyboard:(UIViewController*)viewController
{
//add gesture recognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:viewController action:@selector(dismissKeyboard)];
[viewController.view addGestureRecognizer:tap];
}
- (void)dismissKeyboard
{
//resign first responder
[viewController.view endEditing:YES]; //how can I get the correct VC here?
}
In each view controller I have [Utilities hideKeyboard:self];
I don't know what to do with the gesture recognizer's action.
Is there a way to somehow set the action as a method in the view controller?
Ex: ... action:@selector(VIEW_CONTROLLER_METHOD_HERE)];
I also tried setting the action and passing a parameter so I could have a reference to the current view controller, but I don't know how to pass a parameter in a selector. This also just doesn't fit into a Singleton class, I don't think anyway.
Ex: ... action:@selector(dismissKeyboard:VIEW_CONTROLLER)]; //I know this is wrong, but don't know how to do it correctly
So, is it possible to create the gesture recognizer in a utility class, or am I going in the wrong direction? Let me know if I need to explain anything more clearly.