7

I have a custom View (NotifyView) added on UIWindow with a dismiss button(which remove it from UIWindow). This view is added when a PUSH notification comes in didReceiveRemoteNotification there are several cases when I could be on any screen, and my keyboard is UP via UITextfield/UITextview. At this stage if a push comes, the NotifyView is added on UIWindow behind the keyboard. I want to resign the keyboard once the PUSH is received so for that I could:

  • post a Notification with NSNotificaitonCenter to resign all textfields/textviews (if anyone is firstResponder). For this I have to keep active pointer to the currently active textfield/textview in all controllers.

  • make a variable in AppDelegate and assign the active textfields/textviews to it and on PUSH, and resignFirstResponder of those on PUSH.

Both of the solutions would require making changes to all controller's code and I am looking for something more generic like:

  • could there be any way through which I could simply remove the Keyboard from the screen on receiving PUSH
  • or I could fetch the current firstResponder of the application and resign it explicitly.

these could be generic solutions.

It would be really helpful if someone could facilitate this thought process or someone have any immediate solution for this case.

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
  • Duplicate ? http://stackoverflow.com/questions/1823317/get-the-current-first-responder-without-using-a-private-api – rdurand Apr 26 '13 at 08:33

4 Answers4

7

You can use the method below

[[[UIApplication sharedApplication] keyWindow] endEditing:YES]
vignesh kumar
  • 2,310
  • 2
  • 25
  • 39
4

Try this one , for me it works fine [yourView endEditing:YES]

taffarel
  • 4,015
  • 4
  • 33
  • 61
  • i am looking for one more step generic as for this I have maintain track of my current view to call this. – Saurabh Passolia Apr 26 '13 at 09:12
  • Called to this method will actually call `resignFirstResponder` if there's UITextField in the view. https://developer.apple.com/reference/uikit/uiview/1619630-endediting – Seto Apr 25 '17 at 11:21
0

The problem it is, when you want to give back the focus.

I have implemented as listening each textfield and caching which is the latest. Than resign only that one ( in push ) after that restore state, require focus for that.

0

Try this in delegate method...

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  [self.view endEditing:YES]; // added this in for case when keyboard was already on screen
  [self editStartDate:textField];
  return NO;
}
User-1070892
  • 929
  • 10
  • 16