views:

37

answers:

1

Hi,

i want to set the name of an object like UIButton from a string.

NSString *string = [[NSString alloc] initWithString:@"someString"];
UIButton *(string) = [[UIButton buttonWithType:UIButtonTypeCustom]retain];

My goal is:

UIButton *someString = [[UIButton buttonWithType:UIButtonTypeCustom]retain];

how can i solve this?

+2  A: 

You can't - variable names are resolved by the compiler well before any Objective-C code is executed. What you can do is maintain a map of strings to objects like buttons etc. using NSMutableDictionary:

NSString *string = @"someString";
[buttonMap setObject: [UIButton buttonWithType:UIButtonTypeCustom] forKey: string];

//...time passes...

[[buttonMap objectForKey: @"someString"] setEnabled: YES];
Graham Lee