How to get instance variable name?

2019-08-25 07:00发布

问题:

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

回答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



回答2:

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