本文介绍了如何获取实例变量名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的iPhone屏幕,其中包含两个UIButtons,如下所示。

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];
}

现在按下标题为One的按钮,我想得到变量名称为buttonOne,类似于按下标题为2的按钮我想将变量名称设为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

推荐答案

首先,我想声称这不是一个好的编码风格。我假设你这样做是因为一些特殊/独特的案例,或者作为概念证明。在生产应用程序中,这不是可行的方法。您应该将按钮设置为属性/ ivars,并且可以在按下它们时进行比较,也可以为每个按钮分配标签或单独的目标/选择器。你可以做的任何事情都可以避免这种方法,因为说实话,这种方法很糟糕(请参阅下一段末尾关于nil / 0值的说明)。

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).

您可以从之前的SO答案中查看以下代码 - 它将返回给定指针的ivar名称。但是,您必须将按钮声明为ivars而不是局部变量。此外,如果两个ivars为零,它将报告相同的情况。因此,只有当所有对象ivars都不为nil且原始类型ivars不为0时,这才有效。

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;
}

所以添加方法 buttonPressed:如下:

- (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

这篇关于如何获取实例变量名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 03:29