CodaFi
CodaFi

Reputation: 43330

Longpress On UI Element (Or How To Determine Which Element Is Pressed?)

I have an app that has a rather large help section in PDF form. It's getting to me that the PDF is so large and I can tell in instruments that it's putting a little too much strain on the processor, so I'm going to phase it out.

I figured, a lighter implementation would be using a UILongPressGestureRecognizer that I could attach to every UI element that would display specified text in maybe a popover or a UIMenuController indicating the function of the selected element.

So my question is this: How can I attach something like a tag to EVERY element in the view so it can be passed to a single method? When I tried tags, I found there was no way to access it through the (id)sender section of my method signature and thus there was no way to differentiate between the elements.

EDIT: to the person below: While you have solved my facepalm of a question as to determining the tags of views, how might one go about attaching gesture recognizers to a UIBarButtonItem so as to ascertain it's tag? Your implementations allow for a very nasty unrecognized selector because UIGestureRecognizers dont have a tag property.

Upvotes: 0

Views: 187

Answers (2)

Mark Adams
Mark Adams

Reputation: 30846

You can derive the tag from an object passed in as the sender. Just have to check it's class and cast it appropriately. tag is a UIView property so we'll start there.

- (void)someMethod:(id)sender
{
    if (![sender isKindOfClass:[UIView class]])
        return;

    UIView *senderView = (UIView *)sender;
    NSInteger tag = senderView.tag;
}

Upvotes: 1

Hubert Kunnemeyer
Hubert Kunnemeyer

Reputation: 2261

You can access tags through the -(IBAction)xxxxxx:(id)sender; like so:

NSInteger tagValue = [sender tag];

But why can't you just connect the actions to your elements through Interface Builder? What are the UI elements your using here?

Upvotes: 1

Related Questions