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) -> UITextV开发者_运维问答iew
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.
精彩评论