n.evermind
n.evermind

Reputation: 12004

Is it possible to add a second tag to an UIbutton?

I was wondering if it is possible to add a second tag to a UIButton? I've created a number of buttons programatically in a for-loop and need a reference to the number of the button (e.g. 0, 1, 2) and another reference (integer) in which I store a reference to the page the button links to (e.g. 22, 30, 49). The numbers are not related so I can't determine the first through the second.

This is what I'd like to have:

for (int k=0; k < numberOfTabs; k++) // k < 4 (e.g. 3 < 4) 
{
    UIButton* btn = [[[UIButton alloc] initWithFrame:frame] autorelease];
    btn.tag = k;

btn.tag2 = someReference;

    btn.frame = CGRectMake(-10, 0, buttonWidth, buttonHeight);
    [btn    addTarget:self
            action:@selector(tabAction:)
  forControlEvents:UIControlEventTouchUpInside];
    [btn    addTarget:self
                action:@selector(tabDelete:)
      forControlEvents:UIControlEventTouchDragOutside];
/...

Any suggestions would be very much appreciated.

Upvotes: 4

Views: 1069

Answers (5)

martin&#39;s
martin&#39;s

Reputation: 3955

Why not store the second (any more, if needed) parameters in something like an NSMutableArray?

NSMutableArray *button_to_page = [[NSMutableArray alloc] init];

...

for(...)
{

// Your button creation code

[button_to_page addObject:[NSNumber numberWithInt:my_button.tag];

}

You can get your page number at any time by simply indexing into the button_to_page array. You can also search the array for a page number and get the button index (if needed).

Now, having said that, here you are creating a new NSNumber object for each button's page tag and also carrying around an NSMutableArray to boot. I really think that subclassing UIButton is the way to go. I don't like the idea of encoding stuff into the single tag unless there's a real compelling reason. If you subclass you are still keeping the UIButton pretty lightweight and all your data is encapsulated within the same object very cleanly:

MultiTag_UIButton.h

#import <Foundation/Foundation.h>

@interface MultiTag_UIButton : UIButton {

}
@property (nonatomic, assign) int page;

@end

MultiTag_UIButton.m

#import "MultiTag_UIButton.h"

@implementation MultiTag_UIButton

@synthesize page;

@end

It really is that simple, you don't have to write any code, just add the page property and you are off to the races. Then you can do this:

MultiTag_UIButton *test_button = [[MultiTag_UIButton alloc] init];

test_button.tag = 1;
test_button.page = 23;

NSLog(@"tag %i  page %i", test_button.tag, test_button.page);

[test_button release];

Clean and simple. Realistically you'd have to do a little more in the new class, but you get the idea.

Upvotes: 2

David Neiss
David Neiss

Reputation: 8237

You could subclass, but then when you handle it (if you intermix it with non subclassed buttons), you are going to have to ask the object if it is is the new object type or implements the new accessor to get at it, which is a bit unpolymorphic.

What about if you just leave the class as is, but partition up the existing bits of the tag so that the lower 16 bits are for one purpose and the upper are for your other purpose? Nothing changes interface wise, you just do some masking on the .tag to get your values.

Upvotes: 1

Swapnil Luktuke
Swapnil Luktuke

Reputation: 10475

No you cant. Not directly at least. You can subclass UIButton and add another int property but that seems like an overkill for what you want..

I think it would be better to just decide on how you can fit both the values in the single tag integer...

e.g. if you have pageNumber and buttonNumber you can create the buttons tag like:

button.tag = pageNumber*100 + buttonNumber;

and when you want to which page a button belongs to or what is the index of a button on a page, you can dacode the tag:

int pageNum = button.tag /100;
int buttonNum = button.tag % 100;

Upvotes: 5

Phil Street
Phil Street

Reputation: 2795

According to what I can see looking at Apple's documentation for UIView where the tag property is defined (since UIButton inherits from UIView), it appears you can only have the one.

There's nothing stopping you from subclassing UIButton to add another tag property if necessary as mentioned.

Upvotes: 1

Nick Weaver
Nick Weaver

Reputation: 47241

  • Create a subclass of UIButton with your second tag declared as property.

  • You could as well create an array to map the tag of your button to a page, which would prevent creating a subclass but will introduce some array management method.

I'd prefer the array solution, as I try to prevent subclassing whenever I can.

Upvotes: 3

Related Questions