0

I created a simple iPhone screen with two UIButtons programmatically like below.

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonOne.frame = CGRectMake(60, 70, 200, 40);
    [buttonOne setTitle:@"One" forState:UIControlStateNormal];
    [self.view addSubview:buttonOne];

    UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonTwo.frame = CGRectMake(60, 250, 200, 40);
    [buttonTwo setTitle:@"Two" forState:UIControlStateNormal];
    [self.view addSubview:buttonTwo];
}

Now on press of button with title as "One", I want to get the variable name as "buttonOne", similarly on press of button with title as "Two" I want to get the variable name as "buttonTwo".

I am not finding a way to get the variable name. Any help? Thanks in advance

Vinoth Kumar
  • 156
  • 8
  • 1
    what you want to do by finding variable name? – Pratik Apr 08 '13 at 12:45
  • What do you have against the standard target-action design pattern? – David Rönnqvist Apr 08 '13 at 12:47
  • for debugging purpose, since Macro is been used everywhere for setting text, frame etc, so if there a way to get variable name then it will be helpful – Vinoth Kumar Apr 08 '13 at 12:47
  • you can not get variable name but you can set button tag and from tag you can find which button is clicked. – Pratik Apr 08 '13 at 12:49
  • Setting button tag is something which i found before posting the question, But in a big project with lot of coding already done, it is not possible to set tag for each and every UIControl. I took UIButton with simple example for describing purpose. – Vinoth Kumar Apr 08 '13 at 12:53

2 Answers2

1

First off I'd like to disclaim that this is not good coding style. I assume you're doing this because of some special/unique case, or as a proof of concept. In a production app, this is NOT the way to go. You should set your buttons as properties/ivars and you can compare them when they're pressed, or you can assign tags, or separate targets/selectors for each button. Anything you can do to avoid this approach is good because to be honest this approach is kind of terrible (see note at the end of next paragraph about nil/0 values).

You can check out this code below from a previous SO answer - it will return the name of the ivar given the pointer. However, you have to declare your buttons as ivars and not local variables. Also, if two ivars are nil, it will report the same. So this will only work if all your object ivars are not nil, and your primitive type ivars are not 0.

 #import <objc/objc.h>

- (NSString *)nameOfIvar:(id)ivarPtr
{
    NSString *name = nil;

    uint32_t ivarCount;
    Ivar *ivars = class_copyIvarList([self class], &ivarCount);

    if(ivars)
    {
        for(uint32_t i=0; i<ivarCount; i++)
        {
            Ivar ivar = ivars[i];

            id pointer = object_getIvar(self, ivar);
            if(pointer == ivarPtr)
            {
                name = [NSString stringWithUTF8String:ivar_getName(ivar)];            
                break;
            }
        }

        free(ivars);
    }

    return name;
}

So add a method buttonPressed: as follows:

- (void)buttonPressed:(id)sender {

     if ([sender isKindOfClass:[UIButton class]]) {

          NSString *buttonName = [self nameOfIvar:sender];
          //Now you can do something with your button name
     }
}

Source of first block of code: Get property name as a string

Community
  • 1
  • 1
Jai Govindani
  • 3,181
  • 21
  • 26
  • Working fine. Have to import also. As you said, buttons should be properties, not local variables for this logic to work. I use this purely for debugging purpose only. Thanks – Vinoth Kumar Apr 09 '13 at 05:41
0

we can get only button title name by this way buttonOne.titleLabel.text. we can't get variable name

jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29