Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
745 views
in Technique[技术] by (71.8m points)

access objective-c object by string name or variable

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

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;

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;

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

the result would then be...

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

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;

then at some point

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...