使用字符串作为标签的参考

时间:2014-06-03 04:26:32

标签: objective-c string label

我有一些问题,想知道是否有人可以提供帮助。

我有一个程序,有几个标签被宣布为第1天第2天第3天第4天第5天第6天

for(int i=1; i < 8; i++){
NSString *pickerviewtext = @"Apple";
int valuestock;
NSString *cur = [NSString stringWithFormat:@"%d",i];
NSString *stockname = [NSString stringWithFormat:@"%@%@", pickerviewtext, cur];
valuestock = [[savedStock objectForKey:stockname] intValue];
NSString *key = [NSString stringWithFormat:@"day%d", i];

字符串“key”是标签的名称,我想知道是否可以将与字符串同名的标签更改为变量“valuestock”,如key.text =但不起作用

1 个答案:

答案 0 :(得分:0)

制备

int numLabels = 8;

NSMutableDictionary  *m_labelMap = [[NSMutableDictionary alloc] initWithCapacity: numLabels]; 
// m_labelMap should probably be a member of some class

// add each label to m_labelMap:
[m_labelMap setObject: day1 forKey: @"day1"];
[m_labelMap setObject: day2 forKey: @"day2"];
...

附加在您的代码后面:

// --- searching for an entry ---
UILabel *label = [m_labelMap objectForKey: key];
[label setText: valuestock];