通过字符串名称或变量访问Objective-C对象 [英] access objective-c object by string name or variable

查看:57
本文介绍了通过字符串名称或变量访问Objective-C对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在环顾四周,在执行此操作时并没有发现任何东西,也不确定是否可能,但这是我正在努力实现的目标.请注意,我正在举一个非常简化的示例说明我正在尝试做的事情.同样,所发布的代码相似之处全部是假设的,并不意味着正确的代码语法.它仅旨在通过引用相似类型的功能来帮助进一步说明所需的最终结果.因此,在提供替代方法之前,请先了解概念...

i have been looking around and haven't quite found anything on doing this and am not sure if its even possible, but this is what i am trying to achieve. please note, i am giving a very very simplified example of what i am trying do. also, and resemblance of code posted is all hypothetical and is not meant to correct code syntax. it is only meant to help further illustrate the end result desired by referencing similar type functionality. so please understand the concept before offering alternative methods...

上面已经说过了,正如标题所示,我试图查看我是否可以访问一个对象,让我们仅通过其字符串名称或使用if语句的变量 without 说一个uilabel.例如,假设我们有3个uilabes

with that said, as the title says i am trying to see if i can access an object, lets just say a uilabel via its string name or a variable without using if statements. so for example, lets say we have 3 uilabes

UILabel *label1;
UILabel *label2;
UILabel *label3;

从那里我们进行所有适当的合成,而没有进行任何合成.现在在某个时候,我想参考基于let say和int的标签.

from there we do all the appropriate synthesizing and what not. now at some point i want to reference a label based on lets say and int.

int intNumber = 2;

这是理论部分.我现在想设置名称中具有匹配的int值的标签的文本.

here is the theoretical part. i would like to now set the text of a label that has the matching int value in its name.

label(%i, intNumber).text = [NSString stringWithFormat:@"This is Label %i", intValue];

结果将是...

label2.text = @"This is Label 2";

如果int值为1,则label1将被更改.或者如果它是3,则label3将被更改,依此类推...

if the int value was 1, then label1 would be changed. or if it was 3, then label3 would be changed, etc...

或者是否可以使用标签的字符串名称完成相同的过程.

or if the same process could be done using the string name of the label.

int intValue = 1;

NSString *labelName;

然后在某个时刻

labelName = [NSString stringWithFormat:@"label%i",intValue];

现在可以通过引用"labelName"来设置label1的文本来调用它.如果有人可以提出建议或评论,甚至可以做到这一点,将不胜感激.我知道您可以通过字符串名称引用类和选择器,但是我不确定对象吗?我不知道"valueForKey:"是否是我应该这样做的方式?

now somehow set the text of label1 by referencing "labelName" to call it. if anyone could offer a suggestion or comment to wether something like this can even be done it would be greatly appreciated. i know you can reference classes and selectors by string name, but i'm not sure about objects? i don't know if "valueForKey:" is the way i should be looking to do this?

很长的帖子,很抱歉,但是我只是想确保我说出我想做的事情.

sorry for the long winded post, but i just wanted to make sure i stated what i wanted to do.

推荐答案

抱歉!加尔斯,我终于想通了. "setValue:forKeyPath:"原来是我需要的.所以举个例子...

sorry guys & gals, i wound up figuring this out. the "setValue:forKeyPath:" turned out to be what i needed. so for an example...

example.h

example.h

@property (strong, nonatomic) UILabel *label1;
@property (strong, nonatomic) UILabel *label2;
@property (strong, nonatomic) UILabel *label3;

example.m

example.m

@synthesize label1, label2, label3;

- (void)someMethod:(int)labelVarInt {

[self setValue:[NSString stringWithFormat:@"This is Label %i", labelVarInt] forKeyPath:[NSString stringWithFormat:@"label%i.text", labelVarInt]];

}

希望这可以帮助需要执行类似操作的其他人.

hope this helps anyone else who is needing to do anything similar.

这篇关于通过字符串名称或变量访问Objective-C对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆