Resign First Responder on ScrollView Touch - iphone

How can I hide the keyboard on ScrollView touch event...
Scenario is like that...
->View ->ScrollView ->Textfield
I want to hide the keyboard on touch of scrollView. I tried to override the class for scrollview but still i can't do it.

Doing like this will help:
#interface MyClass <UIScrollViewDelegate> {
}
#implementation
- (void)viewDidLoad {
yourScrollView.delegate = self;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[myTextField resignFirstResponder];
}
If you really want to handle the touch event, then you need to subclass the UIScrollView and override the method:
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
}
More about UIScrollView touch

This is what worked for me
in your viewController's viewDidLoad-method
UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapped)];
tapScroll.cancelsTouchesInView = NO;
[viewScroller addGestureRecognizer:tapScroll];
where viewScroller is your scroller. In the tapped-method we have
- (void) tapped {
[self.view endEditing:YES];
}
Don't know why but the above did not work for me...even though it should

Try this:
Add gesturerecognizer for scrollview,
UITapGestureRecognizer *tapScroll = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped)];
tapScroll.cancelsTouchesInView = NO;
[yourScrollview addGestureRecognizer:tapScroll];
[tapScroll release];
Resign your keyboard in (tapped:) method.

Please, take a look at this answer, This is the easiest one I found.
UitextField resignFirstResponder does not work in scroll view

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
[self.view endEditing:YES];
return YES;
}

Related

UISearchBar doesn't respond to touch event

I am designing an IPhone app using MKMapView and UISearchBar, my issue is i need to make the keypad disappear when i touch the MapView or if i click the cancel button.
I tried using the codes:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[searchBar resignFirstResponder];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
But both dint help.
Here is the ScreenShot:
So please someone help me to resolve the issue. Thanks in advance.
In viewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
In dismissKeyboard:
-(void)dismissKeyboard {
[SearchBar resignFirstResponder];
}
(Where searchBar is the that is responsible for the keyboard)
Here is the updated one that doesn't use Xib file:
.h file:
.m file:
Hope this gives you an insight.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {} is not working

I called the method,
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.name resignFirstResponder];
[self.userName resignFirstResponder];
[self.mailId resignFirstResponder];
[self.password resignFirstResponder];
[self.reTypePassword resignFirstResponder];
}
for the purpose of resign first responder.it is working properly without using scrollview.but if i am using scrollview i cant able to use this method.why?
You can add a gesture recognizer if you need to use a scrollview.
Try this:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(resignFirstResponder:)];
gestureRecognizer.delegate = self;
[scrollView addGestureRecognizer:gestureRecognizer];
-(void) resignFirstResponder:(UITapGestureRecognizer *) gesture
{
[self.name resignFirstResponder];
[self.userName resignFirstResponder];
[self.mailId resignFirstResponder];
[self.password resignFirstResponder];
[self.reTypePassword resignFirstResponder];
}
There are many more alternatives: See
UIScrollView prevents touchesBegan, touchesMoved, touchesEnded on view controller ,
UIScrollView touchesBegan

UIScrollView with a UIButton and UITapGestureRecognizer

I've been playing around with this code, but can't get it right.
Current behaviour: I can scroll around freely on a UIScrollView, even when tapping on buttons. When i tap on something that isn't a button, it calls a method (like i want it to), as a result of a UITapGestureRecognizer. However, i have to hold down on a button for a couple of seconds then touch up for it to trigger the button's UIControlEventTouchUpInside.
What I want to happen: Exactly the same, but to be able to touch the UIButton easily, like I can if i turn off the UITapGestureRecognizer on the view it's sitting on.
Here's my code:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//...
[cell.xButton addTarget:self action:#selector(xButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.delaysContentTouches = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
[self addGestureRecognizer:tapGestureRecognizer];
tapGestureRecognizer.delegate = self;
}
}
- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer
{
[self.header removeKeyboard];
}
As stavash suggested, you could do the following:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
UIView* touchedView = [touch view];
if([touchedView isKindOfClass:[UIButton class]]) {
return NO;
}
return YES;
}
This works in most cases. In addition you may want to check whether the touched view is a descendent of a the scroll view using isDescendantOfView:.
Hope it helps.
I think your answer is UIGestureRecognizerDelegate's method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
Check if the touch's view property is any of the UIButtons you are trying to avoid: if so return NO, otherwise return YES. Don't forget to assign the UITapGestureRecognizer's delegate so it works. Hope this helps.

How to dismiss an UITextView with tapping outside?

I've got a bit of a doubt with UITextViews. I use to put UITextFields, and remove the keyboard by tapping outside with the faithful old:
-(void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UITextField class]])
[view resignFirstResponder];
}
}
I have this UITextView which is set on my view (and actually is self.myTV, to be precise), and I do the following
-(void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UITextView class]])
[view resignFirstResponder];
}
}
Same as above, save for the different class...
Well it doesn't seem to work. Anyone has a suggestion?
An easy way to resign first responder for any subview that is currently the first responder is:
[self.view endEditing:NO];
Or if you want to force the first responder to resign without asking it first:
[self.view endEditing:YES];
The problem is that UITextView is not FirstResponder in your case as per the subview hierarchy of your app.
I would suggest you to categorize UIView to find the first responder as under:
This category on UIView, which calls on the UIWindow and traces for the first responder.
#implementation UIView (FindAndResignFirstResponder)
- (BOOL)findAndResignFirstResponder
{
if (self.isFirstResponder) {
[self resignFirstResponder];
return YES;
}
for (UIView *subView in self.subviews) {
if ([subView findAndResignFirstResponder])
return YES;
}
return NO;
}
#end

Detect single tap on UIScrollView containing 'n' UIImageViews

I am simply trying to get hold of the UIImageView that was tapped on, from the UIScrollView.
I have found 2 ways of achieving the above on the Web.
1st method : create a tap gesture on the uiimageview before adding it to scrollviewer.
This method has not worked for me. the handleSingleTap method never gets called.
I am out of ideas on what I am doing wrong/why this is not working.
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:singleTap];
[singleTap release];
[framesSourceScrollview addSubview:imageView];
[imageView release];
- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
NSLog(#"image tapped!!!");
}
2nd Method : subclass UIScrollView
#interface SingleTapScrollViewer : UIScrollView {
}
#end
#implementation SingleTapScrollViewer
- (id)initWithFrame:(CGRect)frame
{
return [super initWithFrame:frame];
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
// If not dragging, send event to next responder
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event];
}
#end
In the ViewController
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
// Process the single tap here
NSLog(#"Scroll view single tapped. touches count : %d", touches.count);
UITouch *touch = [touches anyObject];
UIImageView *imgView = (UIImageView*)touch.view;
NSLog(#"tag is %#", imgView.tag);
}
Using this method, the touchesDown responder does get called but the '[touches anyobject]' isn't the UIImageView which was tapped on.
I tried setting the 'tag' on every UIImageView I am adding to the scrollview with an increasing counter but I get back 0 no matter which imageview i tap on.
I am new to cocoa in general and I am not sure how to leverage this responder in any other fashion.
Any suggestions/hints would be great.
thanks in advance.
did you set the userInteractionEnabled=YES on the UIImageView. Its turned off by default.