来自Objective-C中的字符串的对象名称 [英] Object name from String in Objective-C

查看:70
本文介绍了来自Objective-C中的字符串的对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从字符串中设置UIButton之类的对象的名称.

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

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

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

我的目标是:

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

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

我该如何解决?

推荐答案

您不能-在执行任何Objective-C代码之前,编译器都会很好地解析变量名.您可以 使用NSMutableDictionary:

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

这篇关于来自Objective-C中的字符串的对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆