Object name from String in Objective-C

后端 未结 1 732

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

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

My

1条回答
  •  伪装坚强ぢ
    2020-12-06 15:04

    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 讨论(0)
提交回复
热议问题