开发者

Object name from String in Objective-C

开发者 https://www.devze.com 2023-01-19 17:53 出处:网络
i want to set the name of an object like UIButton from a string. NSString *buttonName = [[NSString alloc] initWithString:@\"someString\"];

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

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

My goal is:

UIButton *someString = [[UIButton buttonWith开发者_JS百科Type:UIButtonTypeCustom]retain];

how can i solve this?


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

精彩评论

暂无评论...
验证码 换一张
取 消