resignFirstResponder and close keyboard from anywhere? - iphone

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

Related

removing keyboard from Screen without calling resignFirstResponder

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

How can I bring keyboard to edit a field when a UIViewController is shown?

I want to show the keyboard just after a view controller is being pushed to start editing a specific UITextField.
I believe that I should manually fire the event on the ViewDidAppear.
Which is the proper way of doing such tasks?
To make keyboard appear you need to manually set your text field as a first responder:
[textField becomeFirstResponder];
It can be called either in viewWillAppear: or in viewDidAppear: method - whichever provides best behaviour for you.
for that you need IBOutlet UITextFiled *yourTextField;
- (void)viewDidLoad or viewWillAppear:(BOOL)animated or viewDidAppear:(BOOL)animated
{
[yourTextField becomeFirstResponder];
}

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

Unhide the keyboard in iphone

In my application what is want is when user clicks on button next view is pushed and default keyboard should be open in the pushed view .
Thanks in advance
You'll have to use -becomeFirstResponder: in the -viewDidAppear: method.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[yourTextField becomeFirstResponder];
}
This will make the keyboard appear.
Assuming you have a text field or something in that view you need to do this:
[myTextView becomeFirstResponder] in your viewDidLoad method.
in your next view, call the becomeFirstResponder method from the input field.
e.g. [[nextViewController] usernameField] becomeFirstResponder];

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]