IPad dismiss Keyboard without knowing which Textfield opened it - objective-c

Is there a way to do a general resignFirstResponder to hide the keyboard regardless of what textfield/view/etc calls it?
Reason is I have a lot of textfields on my view and don't want to have to resignFirstResponder for all textfields to hide the keyboard. Just want a general
[self resignFirstResponder].
Any ideas?
Thanks in advance

I know that this has already been marked as answered, but for those that run into this like I did you can just use the following method on the view that contains the textfields.
- (BOOL)endEditing:(BOOL)force
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign. UIView Documentation
[self.view endEditing:YES];
it will hide keyboard when we click on view.

You can dismiss the keyboard without any reference to UITextfield / UITextView with help of below code:
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
this will dismiss the keyboard globally without the reference.
hope this will help you.

The easiest way to do this is to have a method for whenever you want to dismiss the keyboard that looks like this:
-(void)dismissKeyboard {
[firstField becomeFirstResponder];
[firstField resignFirstResponder];
}

You can check these questions:
Is it possible to make the iPhone keyboard invisible / remove it without resigning first responder?
Hide Input Keyboard on iPhone Without Knowing First Responder?
In summary:
You can call becomeFirstResponder on some other thing that you choose. It could be a UIViewController or a UIView. I had a similar problem before, I needed to make my keyboard go away when I was pushing my view controller back to its caller, without knowing which textfield was the first responder. Then, on viewWillAppear of my view controller which I was returning back, I called [self becomeFirstResponder] and the keyboard of the pushed view was gone. Because this made whichever text field was it loose being the first responder

In my own app when I had more than one text field and would like to make the keyboard go away regardless which of the fields called it, I would just wrote a method and let each and every of them resignFirstResponder.
I assume that as a programmer, you should have the clear knowledge how many text fields are on your view controller and how you can access them, otherwise it'll get messed up and you app won't look good... :-P

Related

Hiding the UIKeyboard

I am trying to hide the keyboard in my SplitView application (because it covers over part of the root menu). However, the only thing I can find is how to hide the keyboard after a textfield has been used [TextField resignFirstResponder].
Is there any other way to hide the keyboard?
Ideally I would like to use the barButtonItem, which displays the menu, as a trigger to hide the keyboard.
Use this:
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
You need to send the -resignFirstResponder message to the instance of whatever UI element currently has first responder status. So if there were a firstNameTextField property on your class that corresponds to an instance of UITextField, you need to send the message to that object.
[self.firstNameTextField resignFirstResponder];
resignFirstResponder is way to do it. If you have a situation where your firstResponder is not set up as an instance variable (perhaps its generated), you can 'get' your firstResponder using this answer. After you have your first responder object, simply resign it!
hope this helps.

UIPicker with UITextField

I made a research and all posts here are very blury regarding this issue.
I would like to use a UIPicker when pressing on a UITextField.
I would realy appriciate a step by step guide.
I tryd all posts here but every post gives me only a portion of what I need and I can't seem to connect it all together.
This is the last part of my application and i'm going crazy to finish it..
Thank you in advanced!
Gal
There is an inputAccessoryView property that contains a view that will appear instead of a keyboard on the bottom of the screen. Create a UIPicker, adjust its frame, provide values and assign it to the inputAccessoryView property.
UIPicker will appear when user taps on your UITextField.
If you don't need editing, you may use a UILabel instead of the UITextField. Solution is the same. I have a ready-made class if you need.
Here's a way:
http://www.youtube.com/watch?v=t_F1ex5opgA&t=14m10s
-(BOOL)textfieldShouldBeginEditing:(UITextField *)textField
where textField is the name of your text field.
Call your UIPickerView and return NO so that your picker is loaded rather than the keyboard.
The idea is to call an action that opens the UIPicker when the user taps the UITextField. Because the UITextField does not responde to the usual touchUpInside events that UIButtons respond to, I would just overlay a transparent UIButton on top of the UITextField and just in case, make the text field's userInteractionEnabled property NO. Hook the UIButton to responde to touchUpInside and call a method that opens the UIPicker. Another option would be an immediate response to the text field's touch by implementing "textFieldShouldBeginEditing" and immediately resigning the text field.
The next step would be to present the UIPicker - if we are talking about iPad, this would best be done by using a UIPopoverController. On iPhone, maybe consider bringing it up modally. When you create the view controller that holds this UIPicker, be sure to add a delegate property to it so that whatever value that was selected on the picker can be transfered back to the main view controller and on to the UITextField.
Hope this helps with getting you started.

Dismiss a number pad-style keyboard without adding Done key

There is no Done button on a Number Pad-type keyboard. I don't want to add a custom Done button, but how do I dismiss the keyboard?
You could add a UINavigationBar/UIToolBar with a done button(a UIBarButtonItem), and make the textField/textView resignFirstResponder on the done button's action.
You can add the UINavigationBar/UIToolBar as inputAccessoryView of textField/textView.
textField.inputAccessoryView = aNavBarWithDoneButton;
Edit: Availability iOS (3.2 and later)
The simplest solution is to add a new button somewhere in your UI that calls resignFirstResponder on your UITextField (or whatever) when tapped. Putting this in a toolbar is problematic on iPhone because toolbars are typically at the bottom of the screen and obscured by the keyboard.
A slightly more complex solution is to put an invisible UIView behind all of your other tappable UI elements. Any taps not handled by your existing UI will go to this new view, which can call resignFirstResponder on your text field.
If neither of these sound appealing, perhaps you should expand your question to include the type of behavior you want.

UITextfield in UITableViewCell resign FirstResnponder

I have a UITextfield of type NumberPad in a UITableViewCell. How can I make it resign first responder (i.e. dismiss the keyboard)?
Thanks
You have to add a UIButton somewhere in your interface. I often do it on the navigation bar and then resign first responder when it is pressed.
There are all sorts of kludges to superimpose fake done buttons over the numberpad, but they are asking for trouble IMHO. It would be nice if Apple addressed this in future though as lots of people have the same issue.
Good discussion and other solutions here;
How to show "Done" button on iPhone number pad
Implement UITextFieldDelegate and set your UITextField delegate to it. In the -(BOOL)textFieldShouldReturn:(UITextField *)textField delegate method you set the resignFirstResponder call.
You can also set up an action to a button on the pad. You can do this by trying to add a done button to the pad. I know that in the most recent sdk this is somewhat impossible.
For those cases you have two options:
1.Add a done button somewhere on the view. If you are using a navigation controller you can add it to the navigation bar and simply set up the action for that button and in the action you resignFirstResponder for the UITextField.
2.Add your keyboard to a UIActionSheet and add the done button right on top of the keyboard.

UITextView: Must I always resignFirstResponder?

Must I always resignFirstResponder for a UITextView? Or, will this happen automatically when its view controller disappears?
I'm asking because I'm having an issue similar to iPhone Objective-C: Keyboard won't hide with resignFirstResponder, sometimes, where the keyboard stays up even when the nav controller pushes and pops other view controllers. The keyboard works, and when I hit done, it unfocuses the UITextView (i.e., the cursor disappears), but the keyboard stays up.
I never found out why this is happening, but maybe it's due to not doing resignFirstResponder before pushing another view controller, but I thought it was optional?
At a total guess, the UITextView has a reference to the view controller (as its delegate) but does not retain it. When you go to the next screen, the controller is dealloced and then the UITextView (which has perhaps been retained by something else) tries to call back to the dealloced controller and crashes. When you call resignFirstResponder, you reverse the order this happens, and therefore no crash.
The way round this to add a textView.delegate = nil call in your view controller's dealloc method - obviously put it before you release the text view.
The contract between a UITextView and it's delegate says that the delegate will send -resignFirstResponder when the text view is done editing. This informs the framework that the view is done editing, fires the events relating to that (willEndEditing and didEndEditing), and allows other parts of the responder hierarchy to react accordingly. Failing to do so might work, but it's not following the contract (that's all a protocol is) it agreed to.
I don't think you have to because the Xcode Sample UICatalog UITextField doesn't call resignFirstResponder before the TextViewController is popped.
The reason the keyboard got stuck for me is that I was having the same view controller present two view controllers modally at the same time, one after the other. UIKit didn't like that.
Calling resignFirstResponder makes sure that the text property contains the actual text shown in the control.
Depending on the state this is not always necessary, but if your controls have resigned first responder, you know that you're working with valid data.