removing keyboard from Screen without calling resignFirstResponder - iphone

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.

You can use the method below
[[[UIApplication sharedApplication] keyWindow] endEditing:YES]

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

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.

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;
}

Related

How to identify which textfield is currently first responder

If you have several text fields on the screen, and the keyboard pops up for each one when they are tapped, what if you want to programmatically hide the keyboard, or resign first responder, but you don't know which textfield to send the resignFirstResponder message to? Is there a way to identify which object/textField should get this message?
check all of your textfield call
[textfield isFirstResponder]
You could keep track of which text field is the first responder by either setting your view controller to be the delegate object of all text fields and then when your subclassed text fields gets the "becomeFirstResponder" method call, tell your view controller which text field is the current one.
Or, there's a more blunt force approach which is a category extension to "UIView":
#implementation UIView (FindAndResignFirstResponder)
- (BOOL)findAndResignFirstResponder
{
if (self.isFirstResponder) {
[self resignFirstResponder];
return YES;
}
for (UIView *subView in self.subviews) {
if ([subView findAndResignFirstResponder])
return YES;
}
return NO;
}
#end
which I got from this potentially very related question.
You Can Check Your all TextField and than Identify Easily.
[textfield isFirstResponder];
There is no public method to get the current first responder, but you can do things to still get it; The first one is obviously to keep track of this yourself. You can do this in various way and if you don't want to touch any existing class but just want it to work, a category and method swizzling will do the trick. The more cleaner solution however is to iterate through the view hierarchy and ask the views wether they are the current first responder. You can start with the root UIWindow and start iterating, or you can start with your current UIViewController's view, but keep in mind that the current first responder doesn't have to be part of your roots UIWindow view hierarchy (eg. if you have a text field inside an UIAlertView).
Try this (Swift 3):
if textField.isEditing {
textField.resingFirstResponder()
}

resignFirstResponder and close keyboard from anywhere?

I have a situation where the keyboard maybe open and then an NSTimer pops a view over the text view. Is there anyway to close the keyboard globally rather than from the text view resignFirstResponder method? The reason I ask is that the textView is dynamic in that it maybe there sometimes and not others. One way would be to give it a TAG. Can multiple items be referenced with same tag?
I think the answer is no but I would be interested in your thoughts?
Thanks
Steve
To dismiss the keyboard from anywhere, even if you don't know directly who is the firstResponder, use:
[[[[UIApplication sharedApplication] delegate] window] endEditing:YES];
The endEditing: method of UIView should do the trick. Send it to the superview of the potentially existing UITextView when you want to dismiss the keyboard.
You can try to send some control the message becomeFirstResponder
You could pass a reference to the UITextView in the NSTimer...
ORRRRR....
In the view that pops up you could do something like:
for(id view in self.superview.subviews){
[(UIView *)view resignFirstResponder];
}

iPhone SDK: resignFirstResponder not working

For some reason, resignFirstResponder is not working. I am not sure why? I have tried to call it from textFieldDidEndEditing and nothing happens. A NIB is being used and each's delegate is pointing to files owner.
What needs to be done to get the keyboard to dismiss?
Thanks.
Don't use -textFieldDidEndEditing. That's called after the text field resigns firstResponder status, which is what you're trying to use it as a hook to make happen. Cart before horse, chicken-and-egg kind of problem.
Instead use -textFieldShouldReturn to get triggered when the return key is pressed (and remember to return YES; from that.) Also float a clear custom button behind the elements of the view and handle a "background tap" that goes through all the text fields on your view and resigns first responder on the lot of them.
actually you should return NO so that the text field does not begin editing at all. If it does, the firstresponder gets set and the keyboard pops up again.
Make sure your setting your delegates for the textfield.
myTextField.delegTe = self;
And you are using in your header:
<UITextFieldDelegate>
EDIT:
Try:
if(textField == myTextField){
[textField resignFirstResponder];
}

How can I create some view over UIKeyBoard?

I fill information then tap "Done" button for doing some process and show loading view for waiting.
I need loading view is over every view, but How can I do?
Thanks for adviser.
Here is my image (I'm new in here, so I can't post image)
http://www.freeimagehosting.net/image.php?62e2584aea.png
You should make your keyboard disappear from the screen as soon as user taps on done button.
call this method
[myTextField resignFirstResponder]
when you start loading
this is the best answer for your question
[textField setInputAccessoryView:inputAccessoryView];
Did you observe the UIKeyboardWillShowNotification and UIKeyboardDidShowNotification notifications? You can update the view elements in the handlers.
Try to add the loading HUDView ro whatever you are using for the purpose on the application's keywindow.
as
[[UIApplication sharedApplication].keyWindow addSubview:loadingView];
ALso resigning the responder while the loadingview is displayed is really a very important thing you should do.
[myTextField resignFirstResponder]

Focus on next valid key view on iPhone

Is there an iPhone equivalent for the NSResponder methods -selectNextKeyView or -nextValidKeyView from Mac OS X? I know about the -becomeFirstResponder method, but finding out which view to call that on is not very pretty when view hierarchies get more complicated.
There must be some kind of way to find this out as when I press tab when in the iPhone Simulator, focus does properly go to the next UITextField. This made me wonder what exactly happens when I press tab. Any ideas?
Update: This does exactly what I want, but _nextKeyResponder is private API, so a no-no. Is there any way to do a 'fake' tab key press without using private API?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Try to find next responder
UIView *nextResponder = (UIView *)[self.view _nextKeyResponder];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
[self.tableView scrollRectToVisible:[self.tableView convertRect:[nextResponder frame] fromView:nextResponder] animated:YES];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
return NO; // We do not want UITextField to insert line-breaks.
}
There is not a public iOS equivalent for NSResponder's -selectKeyView or -nextValidKeyView.
When the first responder is an instance of UITextField, pressing tab instantiates a private subclass of UIEvent which is passed to -[UIApplication sendEvent:], which in turn calls -[UIView _nextKeyResponder].
-[UIView _nextKeyResponder] doesn't work quite the way you think it does. It treats the key view chain as a loop, so your else block will never be reached. For the same reason, even if there was a public API for synthesizing keyboard events, you probably wouldn't want to use it.
Instead, you probably want something more like UIWebView's UIToolbar-based form input accessory. Its buttons can be enabled and disabled when appropriate, and its delegate handles the actual button press actions.
To implement such a delegate in a general way, however, it might be helpful to look at how -[UIView _nextKeyResponder] is implemented.
In the UITextField delegate -textFieldDidEndEditing:, switch between the various text fields (for example, by testing the text field's tag property).
When you match one text field, set another text field or other control to become the next responder.
I'm surprised nobody else appears to have solved this on iOS.
I devised a solution that handles both Tab and Shift+Tab to go forward and backward to any field you want on iOS, and doesn't use any private APIs.
Here is the write-up: http://weaklyreferenced.wordpress.com/2012/11/13/responding-to-the-tab-and-shift-tab-keys-on-ios-5-ios-6-with-an-external-keyboard/