0

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.

Ryan
  • 1,135
  • 1
  • 13
  • 28
  • Is there a reason you are wanting to do all this in a singleton? Also, when are you lowering the keyboard? For text input? (resignFirstResponder?) – Mark McCorkle May 20 '13 at 21:19
  • For ease, so I just have to call `[Utilities hideKeyboard];` in any view controller to hide the keyboard. `[viewController.view endEditing:YES];` actually resigns the first responder. (I don't know the details) – Ryan May 20 '13 at 21:22

1 Answers1

2

I think you are asking for this but you may be better off just subclassing a viewController and using that throughout the app. Then you could just call [self dismissKeyboard];

+ (void)dismissKeyboard:(UIViewController*)viewController
{
    //resign first responder
    [viewController.view endEditing:YES]; //how can I get the correct VC here?
}

Then call it the same way

[Utilities dismissKeyboard:self];
Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42