How do I make the keyboard go away when the user clicks somewhere else? [duplicate] - uitableview

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Dismiss keyboard by touching background of UITableView
How do I make the keyboard go away when the user clicks somewhere else?
Note: I know how to make the keyboard disappear with sending the resignFirstResponder command to the UITextField. At present the "Done" button is connected to all the correct code to do this and this works.
I have a UITableView with different UITableViewCells, and if the user moves onto another cell I want the keyboard to disappear.
So what events do I also need to include the resignFirstResponder in, for the keyboard to disappear.
Suppose UITableViewCell A has the UITextField, and UITableViewCell B has a button. If the user presses the button in cell B, then I will need to send the command resignFirstResponder back to the UITextField in cell A. First of all the button has no idea which cell it should sent the command to, and second even if the button did know which cell to send the command to how would it?

There's no trivial way to do this. You can put a transparent set of "shield views" all the way around the text field that take up the rest of the screen, and use any touches on them to dismiss the keyboard.

You can create a generic 'hideKeyboard' method in which you can include all text fields that can be first responders. For example,
-(void) hideKeyboard {
[textFieldName resignFirstResponder];
[textFieldSurname resignFirstResponder];
for (UITextField * txtField in arrTextFields) {
[txtField resignFirstResponder];
}
}
Then, at various sections in your class, depending on the functionality required, call;
[self hideKeyBoard];
This simple method means you won't need to keep track of the individual item that 'has the focus' / first responder status.
How to touch any part of the screen to make the keyboard go away
To touch somewhere outside the UITableView and have the keyboard disappear, place an invisible button on top of the 'touch area' that you want to respond to. Then, simply call [self hideKeyboard] from the touch event for that invisible button. Using IB, drag a new rounded button onto your view, then size it to take up the full size of the screen. Next,drag the button up or down the controls list in the IB document window so that button is behind all text fields and buttons, but in front of anything else (like images etc.). Finally, change the type of the button to 'Custom' to make it invisible, but still respond to events. Now all you have to do is to connect the new button's 'touch up inside' event to trigger the 'hideKeyboard' method.
Additionally, see this post for a brilliant solution to dismiss the keyboard when the above solution doesn't work : stackoverflow question 1823317

Related

IOS: Press and drag in button on custom keyboard

I'm building custom keyboard and i want to make response like stock keyboard.
- I can press and hold on one key and drag to other key and so release, the letter will be enter is the one at current finger position
- Can press and hold one key and use other finger to press on the other key and so 2 letters will be entered at the same time.
I've tried many methods in UIButton ( TouchUpInside, TouchDown, TouchUpOutside ... but didn't successed.
You are going beyond the abilities of UIButton. You need to look at creating a custom view which handles Multitouch Events.
It's not easy, but it's not impossible either.

UITextField not receiving touch event

I have a UITextField in a complex view hierarchy. The text field is in a UIView with label and textfield, say, labelfield.
I can type text there.
There is info button on top right corner. When tap on Info, a modal view controller is loaded. When coming back from the modal view controller, the UITextField becomes unresponsive. I cannt type anything there. The labelfiedl is drawn and shown in the screen.
Interesting thing is that, if I press the ok button, and alert is shown and then the textfield becomes active. Also, on top of the view, there is some text with user interaction enable, if I tap and try to select the text and after that the textfield becomes active. Seems I have to do some other activity/touch on the super view, then the text field becomes active.
Why not on the first case it receive any touch event? I tried with textField.enabled, becomesFirstResponder, setNeedsDisplay in the viewDidApper method, nothing works.
Try the following...
Make sure that the super view to which the text field is added as subview large enough to contain text field(ie make sure that no part of text field is out side the superview. check this for all views).
Make sure that there is no view above textfield.use bringSubViewToFront: method.
Try to enable use interaction after a delay
Hope any of the above fix will solve your problem

iOS keyboard flickers when switching view controllers

I have a registration form and I want to have the keyboard always on top.
The way I'm doing it now, is that when the user moves between view controllers, in viewDidLoad, the first UITextField becomes the first responder.
The problem is that the keyboard flickers (disappears and then appears again) when the user moves between view controllers.
Also, related to this: I have a form with a few uitextfields. When the user presses next it goes to the next uitextfield using becomefirstresponder. When the user is in the last textfield, the keyboard button becomes "Done". Then, when the user presses it, if there's an error with the last field, it should get the focus (calls becomeFirstResponder) but that doesn't happen (nothing get's the focus and the keyboard goes down). All the other fields get the focus fine, just this last field doesn't. I've tried about everything: switching to other textfields and back. The problem is that done automatically removes the keyboard.
You should have made two separate questions for this.
First, your flickering:
I'm guessing you're using a UINavigationController. You can add an invisible UITextField somewhere in the UINavigationController, which you give focus before you switch to a new ViewController. Then, when the new ViewController has appeared (viewDidAppear), set the focus to the first textField as you want.
However, the entire approach is kind of hackey and I don't recommend you use it. Instead, try using several views in a scrollView, of which you change the offset when you move to the new view. This will also solve the flickering.
Second, losing firstResponder status on Done:
The done button is specifically there to indicate exactly that which it says; Done. Pressing this assumes the user is finished and that no text is left to type, thus dismissing the keyboard.
If you really want to keep the Done button, then try the following;
Allow the user to dismiss the keyboard.
Upon dismissal, check for the error in the last field.
If there is an error, instead of calling [lastField becomeFirstResponder], try [self performSelector:#selector(thisSelectorWillCallFirstResponder) withObject:nil afterDelay:1.0].
In the method thisSelectorWillCallFirstResponder call [lastField becomeFirstResponder].
This will give time for the keyboard to disappear, before making it pop up again, so it doesn't interfere with the becomeFirstResponder call.
Another method would be to not use a Done button, but instead use the return key. You can intercept return anytime with the delegate method textFieldShouldReturn:. There you can handle any error checking, without causing the textField to lose its focus.

resignfirstresponder on a UITextView inside a UITableViewCell

I am hoping if someone can help me resolve an IOS/XCode question.
I need to have a UITextView created inside a UITableViewCell, this UITextView has responds to a user click, upon which a UIPopoverController will be displayed so that a sub-UITableView is displayed (inside the UIPopoverController) allowing a user to select from a list of choices (lines of text). After the user select the choice (one of the line of text), that line of text will then be displayed inside the said UITextView. First problem I am having is that when the user click on the UITextView the keyboard gets displayed instead of the UIPopoverController. How do I go about disabling ie. calling resignFirstResponder so that instead of the keyboard displaying, I get the UIPopoverController coming up instead. Would someone be kind enough to share similar codes? or point me to some sample of how this can be done? Thanks so much in advance.
You can use following delegate method to detect when textView is tapped and show your popOverController accordingly, return 'NO' in the delegate method so that no keyboard will appear...
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
// code to show popOverController
return NO;
}

Programmatically show the keyboard on iPhone app

I have a view with only one UITextView that is used to enter a value. I want that when the view shows, the textview becomes the first responder (that's the easy part) and also the default keyboard shows up. I tried searching for this in speca but to no avail. There are many posts on how to dismiss the keyboard, but what I want is to show the keyboard w/o waiting for user to touch my textview.
Just setting the text field to firstResponder should do the trick.
Inside your -viewDidLoad:
[myTextField becomeFirstResponder];

Resources