UITextField and tap gesture in leftView - ios

I've an icon on leftView of textField and I want to add tap gesture to it. This is my code :
if (!textField.leftView){
UIImageView *infoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 18)];
infoImage.image = [UIImage imageNamed:#"info"];
infoImage.contentMode = UIViewContentModeCenter;
textField.leftView = infoImage;
textField.leftViewMode = UITextFieldViewModeAlways;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showErrorMessage:)];
tapRecognizer.numberOfTapsRequired = 1;
[infoImage addGestureRecognizer:tapRecognizer];
}
The problem when user click on the image on leftview nothing is happend and the showErrorMessage is not called .
Update
The tap issue was solved by enabling userInteraction on the infoImage.
But now I'm getting [UITapGestureRecognizer tag]: unrecognized selector sent to instance at second line of my Show errorMessage :
-(void)showErrorMessage:(id)sender{
UIImageView *icon = (UIImageView *)sender;
NSString *key = [#(icon.tag) stringValue];
NSLog(#"%#", [_errorDictionary objectForKey:key]);
}

Missing infoImage.userInteractionEnabled = YES;

you have to set the image's property userInteractionEnabled to TRUE

Related

iOS UITapGestureRecognizer not working sometimes

Sometimes the selector method "tagClickAtIndex" not getting called.
UILabel* label = [[UILabel alloc] init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
[label addGestureRecognizer:tapGesture];
-(void)tagClickAtIndex:(UITapGestureRecognizer*)gesture
{
NSMutableDictionary *mutDict = [[[NSMutableDictionary alloc]initWithDictionary:[_tagArray objectAtIndex:gesture.view.tag]] mutableCopy];
[mutDict setValue:[NSNumber numberWithLong:gesture.view.tag] forKey:#"index"];
[self.delegate tagClickAtIndex:mutDict];
}
you forget to set the frame of label, the reason it only define the clickable area
label.frame = CGRectMake(0,0,200,30)
[yourMainview addSubView:label]
and set if you need
tapGesture.numberOfTapsRequired = 1;
If you are programmatically creating UIlabel,your above code is not working.I tried your code.
UILabel* label = [[UILabel alloc] init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
[label addGestureRecognizer:tapGesture];
It does not show anything.As you have not set the frame and add label view,it does not show.
So Then I tried creating UILabel freshly.I set frame,addSubView to self.view and I set the color as well as number of taps for label.
UILabel* label = [[UILabel alloc] init];
label.frame = CGRectMake(30, 100, 200, 100);
label.text = #"click me";
label.textColor = [UIColor blueColor];
[self.view addSubview:label];
Now Tap gesture recognizer for label
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tagClickAtIndex:)];
tapGesture.numberOfTapsRequired = 1;
label.userInteractionEnabled = YES;
[label addGestureRecognizer:tapGesture];
in delegate method I put log,It calls everytime whenever I touch the label
-(void)tagClickAtIndex:(UITapGestureRecognizer*)gesture
{
NSLog(#"The tap is called");
.........
}
The printed statements shows
The tap is called

I don't get why this #selector isn't working

I am creating several UIView's which I add a UITapGestureRecognizer with an action thats suppose to call a method in the viewController, but it doesn't seem to work.
-(void)setupFreeMoneyView {
NSArray *array = #[#"Facebook", #"Twitter", #"SMS", #"Email"];
int index = 0;
for (NSString *str in array) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100*index++, 0, 100, 100)];
[imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#Logo", str]]];
[imgView addGestureRecognizer:[[UITapGestureRecognizer alloc]
initWithTarget:self
action:NSSelectorFromString([NSString stringWithFormat:#"postTo%#", str])]];
[freeView addSubview:imgView];
}
}
freeView is a UIView created in the MainStoryboard.
Ive checked if its the NSSelectorFromString() by using a normal #selector() thats not working but it isn't the cause.
It does work if I change the line [imgView addGestureRecognizer:... to [freeView addGestureRecognizer:...]
I just don't seem to understand why this is happening.
try this
imgView.userInteractionEnabled = YES ;
userInteractionEnabled of UIImageView is set to NO by default.
maybe you need set userInteractionEnabled = YES for the UIImageView

TapGesture recognizer on multiple UIImageView not working

TapGesture recognizer on multiple UIImageView is not working, while it detects last added imageviews gesture.. I have done this,
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
tapped.delegate = self;
UIImageView *sample_book1= [[UIImageView alloc]initWithFrame:CGRectMake(70, 135, 100,125) ];
sample_book1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"mathematics.png"]];
sample_book1.userInteractionEnabled = YES;
sample_book1.tag = 0;
[sample_book1 addGestureRecognizer:tapped];
[self.view addSubview:sample_book1];
UIImageView *sample_book2= [[UIImageView alloc]initWithFrame:CGRectMake(220, 135, 100,125) ];
sample_book2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"biology.png"]];
sample_book2.userInteractionEnabled = YES;
sample_book2.tag = 1;
[sample_book2 addGestureRecognizer:tapped];
[self.view addSubview:sample_book2];
UIImageView *sample_book3= [[UIImageView alloc]initWithFrame:CGRectMake(370, 135, 100,125) ];
sample_book3.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"chemistry.png"]];
sample_book3.userInteractionEnabled = YES;
sample_book3.tag = 2;
[sample_book3 addGestureRecognizer:tapped];
[self.view addSubview:sample_book3];
The tap gesture is not working in sample_book1,sample_book2.... it's only working in sample_book3.. What i'm doing wrong..
As borrrden said, when trying to track gesture, each view must have its own gestureRecognizer.
For each of your sample_books, you should use
[sample_bookX addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(myFunction:)]];
instead of trying to add multiple times the same GR
The argument received by myFunction: would then be the proper tapGR and you could get to the tapped imageView by calling sender.view (providing your myFunction signature look like
- (void) myFunction:(UIGestureRecognizer *)sender
Cheers,
What you are doing wrong is trying to use the gesture in a way that it is not supposed to be used. A gesture can only be attached to one view. You need to make a new one for each view.

(WeeLoader) 'UITapGestureRecognizer' was not declared in this scope

I know that when the error is "'blah' was not declared in this scope" means when the object was not created correctly, but when I am making a Notification Center widget for iOS, using the WeeLoader template and THEOS to compile, I am getting this error: 'UITapGestureRecognizer' was not declared in this scope.
Here is my .mm file:
- (void)loadFullView {
UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
UIImage *bg = [[UIImage imageWithContentsOfFile:#"/System/Library/WeeAppPlugins/WeeAppTest.bundle/WeeAppBackground.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:71];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];
bgView.frame = CGRectMake(0, 0, 316, 71);
bgView.userInteractionEnabled = YES;
[bgView addGestureRecognizer:Tap];
[_view addSubview:bgView];
[bgView release];
[Tap release];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 316, 71)];
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.text = #"Hello world";
lbl.textAlignment = UITextAlignmentCenter;
[_view addSubview:lbl];
[lbl release];
}
-(void) handleTapGesture:(UIGestureRecognizer *) sender {
}
What am I doing wrong? I am pretty sure that I am declaring everything correctly, by the way, the code does work with out the UITapGestureRecognizer.
Thank you.
I think there are a few things going on here.
I think you could be getting the out of scope error because the gesture recognizer should be declared in the viewDidLoad or the init methods.
Also, in the UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)]; You call your method handleSingleTap:
Therefore your method should be -(void) handleSingleTap:(UITapGestureRecognizer *) sender

Detect tap on UIImageView inside UIScrollView

I have a horizontal scrollview filled with UIImageViews.
I want to detect a tap on the UIImageView and have its background color changed.
Somehow the tap gesture is not working or so.
However, when I add a tap gesture to the scrollview, it works. The scrollview.background color can be changed.
But I want to detect a tap on the UIImageViews it contains!
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 768, 127)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor orangeColor];
[scrollView setShowsHorizontalScrollIndicator:NO];
UIImageView *contentOfScrollView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 1, 1130, 125)];
scrollView.contentSize = CGSizeMake(contentOfScrollView.frame.size.width, contentOfScrollView.frame.size.height);
for (int aantal=0; aantal < 6; aantal++) {
UIImageView *item = [[UIImageView alloc] initWithFrame:CGRectMake(3+(aantal*188), 0, 185, 125)];
item.backgroundColor = [UIColor yellowColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:#selector(imageTapped:)];
tap.numberOfTapsRequired = 1;
tap.cancelsTouchesInView=YES;
item.userInteractionEnabled = YES;
[item addGestureRecognizer:tap];
[contentOfScrollView addSubview:item];
}
//UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
//[scrollView addGestureRecognizer:tap];
scrollView.userInteractionEnabled=YES;
scrollView.delaysContentTouches=NO;
[scrollView addSubview:contentOfScrollView];
[self.view addSubview:scrollView];
And this is the imageTapped function.
-(void)imageTapped:(UITapGestureRecognizer *)gesture
{
NSLog(#"tapped!");
gesture.view.backgroundColor = [UIColor whiteColor];
}
User interaction is set to NO by default for UIImageView, so you need to set it to YES.
You set it to yes for "item", but not for contentOfScrollView.
Your error is here:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:item action:#selector(imageTapped:)];
You need to change the target to "self" instead of "item", then it won't crash.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];

Resources