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
Related
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
How to resign a UITextField of UISearchBar? I tried with the below code
UIView * subView;
NSArray * subViews = [self.searchBar subviews];
for(subView in subViews)
{
if( [subView isKindOfClass:[UITextField class]] )
{
((UITextField*)subView).delegate=self;
((UITextField*)subView).returnKeyType=UIReturnKeyDone;
break;
}
}
for (UIView *subview in self.searchBar.subviews)
{
if ([subview conformsToProtocol:#protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeWhileEditing];
}
}
And added the TextField Delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
this works but this resigns the keyboard oly if the textfield is not empty. If the textfield is empty then it is not resigning
Use this :
[searchBar resignFirstResponder];
Please do this one...
-(void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
if (searchBar.text.length == 0)
[searchBar resignFirstResponder];
}
i hope this will work fine.
First set Delegate in .h file like bellow...
#interface yourViewController : UIViewController<UISearchBarDelegate>
and when you want to resign keyboard of UISearchBar simple use bellow line..
[searchBar resignFirstResponder];
UPDATE:
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
[self.searchBar resignFirstResponder];
return YES;
}
I am currently at topViewController while a UIView is added to my rootViewController. Is there any way that I can remove all UIViews before going to rootViewController via popToRootViewContoller?
In Going back action,type following code:
for (UIView *subview in self.view.subviews) {
[subview removeFromSuperview];
}
And then type popToRootViewContoller.
Add this in viewWillAppear: method of YourViewController
for (UIView *view in self.view.subviews) {
[view removeFromSuperview];
}
Hope it helps you
In viewWillDisappear you can do this task, when you are leaving your rootViewController then this method will call , so you can remove your view at this point.
-(void)viewWillDisappear:(BOOL)animated
{
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
}
Try this:
-(void)viewWillAppear:(BOOL)animated
{
NSArray *subviews = [self.view subviews];
for (UIView *subview in subviews) {
[subview removeFromSuperview];
}
}
[yourView removeFromSuperView]
but added subviews willnot be there when you pop a UIViewController, so i don't think you need to do this
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.
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;
}