Reputation: 11
Is it possible to use a button's tag to set a variable?
Here's what I mean:
Clicking a button retrieves its tag which will also be a variable.
Then open an NSPanel, let the user select a file and click OK, which saves the path of the selected file to a string.
Then set the string of the path to the corresponding variable...
I think I'm asking this correctly. I'm just starting out so please don't down-vote me, just ask for clarification and I'll provide it.
I think all I need to know how to do is query a button for it's tag... but I've googled it for quite a while and haven't been lucky yet.
Additions:
Here's what I'm trying to do.
Lets say I make a small array:
NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:@"blank"];
[myArray addObject:@"blank"];
[myArray addObject:@"blank"];
I have a 3 buttons, each with tags 0, 1, and 2, respectively.
I also have a function, lets say setPath.
What I would like setPath to do is use the tag of the button, so if I click buttonOne, who's tag is 0 it would set the object at index 0 with a path that is stored using an NSOpenPanel but if I click on buttonTwo, who's tag is 1, it would set the object at index 1 using a path that NSOpenPanel gets.
I think that's a better description...
Upvotes: 0
Views: 2555
Reputation: 19251
Since tags are int values, you could map tags to NSString objects with an NSDictionary:
self.pathsForTags = [[NSDictionary alloc] initWithObjectsAndKeys:
@"/path/one", [NSNumber numberWithInt:1],
@"/path/two", [NSNumber numberWithInt:2],
@"/path/three", [NSNumber numberWithInt:3],
nil];
- (IBAction)buttonClicked:(UIButton *)sender
{
NSNumber *key = [NSNumber numberWithInt:sender.tag];
NSString *path = [self.pathsForTags objectForKey:key];
// Use path here
}
It would be even easier to use the indices of an NSArray (as Grady Player suggested) rather than an NSDictionary but you would have less control over the possible tag values (they would have to be consecutive starting at 0).
Upvotes: 0
Reputation: 14549
You have a couple of options, and I do this from time to time, it is just about the only way to do it if you have a massive amount of NSResponders sending the same action.
-(IBAction)someAction:(NSButton *)sender
{
switch(sender.tag)
{
//one possibility
case 0:
//do something
break;
}
//another possibility
id objectOfInterest = [someArray objectAtIndex:sender.tag];
}
Upvotes: 0
Reputation: 3352
A view's tag is an NSInteger
. From your description it sounds like you either believe a tag can be an NSString
, or you want to set the button's tag
to be a pointer to an NSString
, which you'd pull out of the sender
's tag in the button's action method.
I suspect you're just confused about what a view's tag
can contain, but if you were considering setting the tag value to a pointer to an object, I advise against it. It's an anti-pattern, and fragile to boot.
You're much better off rethinking what you're trying to do. Is the value you get back from the NSOpenPanel
or NSSavePanel
something you want to hold on to as long as your object exists? Or is it something you need just long enough to process the value?
If you just need the path to, say, read the contents of a file, a local variable is perfectly adequate. If you need the value indefinitely, use a property.
Upvotes: 0
Reputation: 77291
I'm not sure if this is what you want, but you can take the value from the tag field and assign it to a variable, ie:
int a = myButton.tag;
The tag field is just an int that is part of the UIButton class.
Upvotes: 1