代码之家  ›  专栏  ›  技术社区  ›  Retterdesdialogs

Objective-C中字符串中的对象名

  •  4
  • Retterdesdialogs  · 技术社区  · 14 年前

    我想从字符串中设置uibutton等对象的名称。

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

    我的目标是:

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

    我怎么解决这个问题?

    1 回复  |  直到 12 年前
        1
  •  6
  •   user23743    14 年前

    在执行任何Objective-C代码之前,编译器就不能很好地解析变量名。你什么 可以 Do是使用 NSMutableDictionary :

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