iOS 5: Hide keyboard in UITableViewController with static cells and textfields - iphone

I'm developing an app that has an UITableViewController with static cells. Those static cells are custom ones and have UITextFields within them. If the user touches one of those textfields, the keyboard pops up. So, after that, what I need to do is to be able to dismiss the keyboard by touching anything on the background. By anything I mean the table view background, its cells and the content of those cells (like the text fields for example).
I used to do this by placing a View in the front of all my other subviews and capture the Touch Inside Up event to dismiss the keyboard. Unfortunately, I can't do this this time because I'm using a UITableViewController and I can't switch it back to an UIViewController because I've already done a lot of work with those static cells.

-(void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event{
[yourTextField resignFirstResponder];
}
Try that.
EDIT
Hi,
I've got your answer :
-(BOOL)isFirstResponder
When you enter something in a text field, it becomes first responder and the keyboard appears. You know that the keyboard is active if [textField isFirstReponder] returns YES.
You may also see that link.may be it will help you.
how to hide the keyboard when empty area is touched on iphone

Well, I found the answer myself. What I need to do was just place programatically an UIView in the foreground and do as I said in the original post. I couldn't do this before because I was trying to achieve that from the Interface Builder.

Related

iPhone app: UITableViewController with UITextView in a cell, can't get rid of keyboard

I know this question has been asked before but I couldn't find an answer that applied to my problem.
I've got a UITableViewController that has a third row that is filled with a UITextView.
I'm quite happy with the way it looks and the way text is typed into it.
However I'm unable to find a way of getting rid of the keyboard once the user is done entering text. I'd like to be able to use the return button for actual \n in the text.
I've gotten this far that pressing the upper two rows will make the textView te resignFirstTransponder but is there a way to catch a tap on the greyish background?
This is all in a UITableViewController loaded from a nib file.
Btw, I'm quite new to iOS programming so the more elaborate your answer the better :)
Thanks!
A pattern many apps follow is to show a horizontal bar with buttons on it just above the keyboard. It can contain a done button clicking on which you can hide the keyboard. And of course you will have to create that horizontal view yourself.
Another way would be to enable a touch recognizer elsewhere, and on a tap outside hide the keyboard
One alternative would be to add a toolbar to the keyboard with something like a "done" button that will dismiss it. You can find some sample code about that here. One second approach would be to dismiss the keyboard when the user selects a different cell or even when the tableView scrolls. In order to do that, you can add relevant code in -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath or in -(void)scrollViewDidScroll:(UIScrollView *)scrollView respectively.
This can get a little tricky if your new at iOS. The way I handle a UITextView in a UITableViewCell is I make a custom UITableViewCell subclass with an outlet for the UITextView.
Then set your cell in interface builder to be of that subclass. In the CellForRowAtIndexPath:
set the delegate to self. Then in the DidSelectRowAtIndexPath you call the delegate for the TextView based on the indexPath, this way the keyBoard will dismiss for the correct row if you touch the background. If you want it to dismiss when the user touches any cell just call the delegate without specifying the indexPath. The Delegate is TextViewShouldEndEditing:.Ill post some code if you want.

hide keyboard by dragging finger like sms app in iOS 5

I have a chat feature in my app and I am trying to hide the keyboard by dragging the finger downward just like how you can hide the keyboard in the sms app in IOS 5.
I have subclassed UITableView, however as soon as scrolling starts I no longer get calls to
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
I am wondering how I can get scrolling and find out where the finger is during scrolling so that if it starts to get close to the keyboard I can start to hide it.
I think this is a feature lots of people will want, any ideas on how to make it?
DAKeyboardControl it an MIT Licensed code source for what you are looking for.
Further googling revealed that this question is a duplicate of:
How to move iPhone keyboard down like in Messages.app?
I had enabled this feature in my app, with the help of Daniel's DAKeyboardControl library. You can implement the iMessage style keyboard hiding acidity with just one line of statement:
[self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) {
// Move interface objects accordingly
// Animation block is handled for you
}];
UITableView inherits from UIScrollView, so you can detect the scroll position with UIScrollViewDelegate methods like - (void)scrollViewDidScroll:(UIScrollView *)scrollView
Luckly in iOS 7 Apple added the handy property keyboardDismissMode on UIScrollView. Now your app can behave like Messages just by changing a single property on your Storyboard, or by adding a single line of code.
This property uses the new ScrollViewKeyboardDismissMode enum. The possible values for the enum are as follows.
UIScrollViewKeyboardDismissModeNone
UIScrollViewKeyboardDismissModeOnDrag
UIScrollViewKeyboardDismissModeInteractive
Here is the storyboard property do dismiss the keyboard on scroll

show subview when UITextField touched

I'm pretty new to iPhone development, so please excuse my ignorance. I've googled this a bit and so far come up with nothing useful. Here is what I would like to do:
I would like a subview to popup(with the rest of the screen showing in the background) when a UITextField is touched. The popup is a calculator UIView that I created in the IB. It seems it is better to have a popup show than a customized keyboard, due to Apple's developer guidelines.
Here is my question. How do I capture the touch on the UITextField and how do I show the subview?
I have tried things like below to show the subview, with no luck:
CustomCalculator *customCalc = [[CustomCalculator alloc] initWithNibName:#"CustomCalculator" bundle:nil];
UIViewController *calcController = [self.customCalc.view];
[self.view addSubview:calcController.view];
Use the delegate method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//Add your subview here
return NO; //this will stop the keyboard from poping up
}
This way when someone taps the textfield, your view will popup instead of the keyboard.
Now once the user is interacting with your view, you will have to manipulate the string in the textfield.text property directly as a reaction to the User tapping buttons in your view.
Implement the UITextFieldDelegate and the method for it is
-(void) textFieldDidBeginEditing:(UITextField *)textField
The above method is fired when you touch the UITextField. You may then position the UIPopoverController (I'm guessing that is what you're using to show the view in a popup) and as soon as you're done there pass the values back to the UITextField. Hence the popover's/viewcontroller presented's delegate should be your textfield object.
EDIT: After seeing the other answer below it struck me that I forgot to tell you how to stop the keyboard from showing. Just make this the first line in the method I've mentioned above:
[textField resignFirstResponder];

resignFirstReponder by touch somewhere outside the UITextView with nested View hierarchy

my first question on stackoverflow...i'm very nervous ;)
Ok, i have troubles to make my UITextView resignFirstResponder on the moment i touch some where on the rest of my view hierarchy.
It's like this:
UIView -> UITableView -> UITableCell -> UIView (my containerview) -> UIView (my bubbleview) -> UITextView
As the UITextView isFirstResponder, we can fill in some text, and keyboard is shown. It would be nice if we do not have to dismiss the keyboard by pressing a button, but just by tapping outside the UITextView somewhere on the surface.
But how and where should i respond to a tap gesture? And on that controller somewhere in the chain. I don't have everywhere a reference to the UITextView to send it a message (resignFirstReponder) to...
I hope i can make myself clear. How can i get rid of of the keyboard by just touch somewhere on the surface....
Hopefully somebody can give some hints...
Regards,
Jeroen
You could use NSNotificationCenter to fire a notification when you touch outside of the text fields bounds to which to your text fields respond to with resignFirstResponder.
Edit with more detail:
You could implement the following method on your UIViewController/View a
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[myTextFeild resignFirstResponder];
...or send a notification etc.
}
Its a bit of a cheap way to pull it off but does work.
You might try subclassing UITextField and overriding pointInside. Examine the touch received and if it is outside of your UITextField subclass instance, then resignFirstResponder.
Edit:
The above actually won't work and I apologize for the bad advice. What I would suggest instead, is adding a transparent subview over the window, which overrides hitTest and calls a predefined callback but returns nil. In the callback, you could perform some arbitrary task (like resigning first responder from your text view) and then remove the view from the hierarchy.
There is a class named CWPrimaryViewWindow in my open source project at https://github.com/Jayway/CWUIKit that is a helper class for implementing this behavior.
The primary idea is to subclass UIWindow in order to hijack all touch events. Then inhibit all touch events not targeted for the primary view, instead firing a cancel notification. While passing through touch events to the primary view as normal.

Tapping a UIScrollView to hide the keyboard?

(I posted this before anonymously, but then couldn't use the same computer again, so I'm posting it from my account now. Sorry to the guy who answered before.)
I'm working on an iPhone app which involves typing stuff into a UITextView, which adds content to a UITableView. The problem is, I need to be able to close the keyboard when the user's done with it, and the only area that is really visible other than the keyboard and UITextView at this point is the UITableView. I'm having trouble implementing a touch event on the UITableView (as in, touching the UITableView anywhere, not just didSelectRowAtIndexPath:). Here's the code I'm using in the view controller, but for some reason, it's not being executed at all:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[textView resignFirstResponder];
}
Any suggestions?
"Here's the code I'm using in the view controller"
That's your problem - you have to create a subclass of UITableView and put the function in there before you will get those touch events.
Put an invisible button over the UITableView and trap the taps.
Yeah, I just said put a breakpoint in to make sure resignFirstResponder is actually being called and that it is being called on the correct text field.