The name of an object from a string in Objective-C

I want to set the name of an object, such as UIButton, from a string.

NSString *buttonName = [[NSString alloc] initWithString:@"someString"];

My goal:

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

how can i solve this?

+4
source share
1 answer

You cannot - variable names are resolved by the compiler long before Objective-C code is executed. What you can do is save a line map for objects like buttons, etc. Using NSMutableDictionary :

 NSString *string = @"someString"; [buttonMap setObject: [UIButton buttonWithType:UIButtonTypeCustom] forKey: string]; //...time passes... [[buttonMap objectForKey: @"someString"] setEnabled: YES]; 
+6
source

All Articles