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

5
  • 1
    what you want to do by finding variable name? Commented Apr 8, 2013 at 12:45
  • What do you have against the standard target-action design pattern? Commented Apr 8, 2013 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 Commented Apr 8, 2013 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. Commented Apr 8, 2013 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. Commented Apr 8, 2013 at 12:53

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Working fine. Have to import <objc/runtime.h> 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
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.