Question

I am wondering if anyone has used the userInteractionEnabled method on a UILabel to allow the label to act like a button (or just to fire off a method). Any help would be greatly appreciated. Cheers!

Update (4/30/09 @1:07pm) Clarification: I have a standard InfoButton and next to it I want to place a label with the text "settings" and I would like the label to function like the button (which flips over to a settings screen. So, basically I need to tie the already defined showSettinsView to the "infoLabel" label; a user clicks on the infoButton or infoLabel and the method fires off.

The infoButton is already working and is using an IBAction to trigger the method. I would like to know how to wire up the label to implement the same method. That is all. Cheers!

Was it helpful?

Solution

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.

OTHER TIPS

Another solution could be to use a UIButton with its type set to custom, instead of a UILabel. This way your second button will look like a UILabel, and you can configure it to trigger the showSettingsView method.

I agree with Alex Apr 30, '09 and my proposal is just an addition to it, if you don't want to go with a UIButton. The way I solved it was by creating a UILabel through

- (id)initWithFrame:(CGRect)frame
{
    label = [[UILabel alloc] initWithFrame: CGRectMake(5, 5, 20, 20)];
    ...
    [label setUserInteractionEnabled: YES];
    [self addSubview: label];
}

then you can override the touches* methods like touchesBegan, touchesEnded with something like

- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
      UITouch *touch = [touches anyObject];
      if([touch view] == label) {
          NSLog(@"I've been touched");
       }
   }     
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top