Handling Touch Event in Uilabel and Hooking It Up to an Ibaction

Handling Touch Event in UILabel and hooking it up to an IBAction

Check it out:

UILabel *label = ...
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(labelTap)];
[label addGestureRecognizer:tapGesture];

The trick is to enable user interaction.

Is there a touch method for UILabel?

You could use a gesture recognizer:

- (void)someSetupMethod {
// ...
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = \
[[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(didTapLabelWithGesture:)];
[label addGestureRecognizer:tapGesture];
[tapGesture release];
}

- (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
// ...
}

How to make a UILabel clickable?

Have you tried to set isUserInteractionEnabled to true on the tripDetails label? This should work.

UILabel: Using the userInteractionEnabled method on a label

userInteractionEnabled is not a method but a property. But I think you will want to set this to YES to allow events to get through to the UIView superview.

What you might want to do is override the touchesBegan:withEvent: method of the UIView that contains your UIButton and UILabel subviews.

Within this method, test if any of the UITouch touches fall inside the bounds of the UILabel.

That is, does the CGPoint element [touch locationInView] intersect with with the CGRect element [infoLabel bounds]? Look into the function CGRectContainsPoint to run this test.

If so, then fire off an NSNotification that calls the same IBAction selector as the UIButton.

Input text to tapped UILabel?

Your approach ought to work. Something is wrong with a detail. I suspect that you forgot to setup the labels to receive touches (they don't by default). It ought to work as simply as this...

// MyViewController.m

@property(weak, nonatomic) IBOutlet UILabel *labelA; // presumably these are painted in IB
@property(weak, nonatomic) IBOutlet UILabel *labelB;

// notice no gesture recognizer ivars here

// @implementation ...

- (void)viewDidLoad
{
[super viewDidLoad];

UITapGestureRecognizer *tapA = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self.labelA addGestureRecognizer:tapA];

// You can set this in IB, but it must be set somewhere
self.labelA.userInteractionEnabled = YES;

UITapGestureRecognizer *tapB = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self.labelB addGestureRecognizer:tapB];
self.labelB.userInteractionEnabled = YES;
}

Notice two things: (1) we set userInteractionEnabled = YES on the labels, and (2) there are two gesture recognizers, one to do a job for each label. We don't need ivars for them. They are where they need to be; attached to subviews. (you can always get them by saying self.labelA.gestureRecognizers, but I rarely find the need to in practice)

- (void)tapped:(UIGestureRecognizer *)gr {

UILabel *label = (UILabel *)gr.view;
NSLog(@"the label tapped is %@", label.text);
}

Notice the form of this method matches @abbood's suggestion. The first param is the gr, and can be accessed this way, without use of an ivar. This runs fine in my Xcode.

linking UIProgressView with IBAction

Add a gesture recognizer. With UITapGestureRecognizer you can make any UIView (or subclass) instance respond to taps. You'll have to do it in code instead of in IB, which means you'll need an IBOutlet connection to the progress view. You might need to set userInteractionEnabled to YES to make it work.

It's kind of hard to imagine a scenario where this makes sense, but I'll just assume you have a good reason and leave it at that.

Question about Event of UILabel

UILabel is a subclass of UIResponder. You can override the these following methods to handle touches.

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:


Related Topics



Leave a reply



Submit