Tapping a UIScrollView to hide the keyboard? - iphone

(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.

Related

touch events on UITextField rightView

I have several text fields which compromise a registration form . When the user hit submit and made invalid inputs the wrong fields are marked with an icon as rightView.
The rightView i use is a custom UIImageView subclass which has it's:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
overwritten to display a alert to tell the user what exactly is wrong with this field.
The problem is that the touchesBegan method is never called. Why is the view not receiving touch events when I touch it?
you need to enable user interaction of images to TRUE
[imageView setUserInteractionEnabled:YES];
You should check whether your view is on the upper level of views (that it's not overlayed by another transparrent view)
And also check for view's user interaction set to ENABLED
Also check that you tap directly on the view, this method is called only when you tap within your view's bounds
I also recommend you to read a bit about UITapGestureRecognizer class may be this would be more convenient way to use...

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

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.

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.