我從通過標籤迭代
@property (strong, nonatomic) IBOutlet UILabel *lastNumber0;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber1;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber2;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber3;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber4;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber5;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber6;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber7;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber8;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber9;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber10;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber11;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber12;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber13;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber14;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber15;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber16;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber17;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber18;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber19;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber20;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber21;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber22;
@property (strong, nonatomic) IBOutlet UILabel *lastNumber23;
起一堆標籤,而這些都是爲了要通過一個for循環這裏迭代的基礎上通過;
for (int d = 0; d < [lastNumbers count]; d++) {
int value = [[lastNumbers objectAtIndex:d] intValue];
if ([red containsObject:[NSNumber numberWithInteger:value]])
{
self.lastNumber0.text = [NSString stringWithFormat:@"%i", value];
self.lastNumber0.textAlignment = NSTextAlignmentLeft;
self.lastNumber0.textColor = [UIColor redColor];
}
if ([black containsObject:[NSNumber numberWithInteger:value]])
{
self.lastNumber0.text = [NSString stringWithFormat:@"%i", value];
self.lastNumber0.textAlignment = NSTextAlignmentRight;
self.lastNumber0.textColor = [UIColor blackColor];
}
if ([green containsObject:[NSNumber numberWithInteger:value]])
{
self.lastNumber0.text = [NSString stringWithFormat:@"%i", value];
self.lastNumber0.textAlignment = NSTextAlignmentCenter;
self.lastNumber0.textColor = [UIColor greenColor];
}
}
}
希望這是有道理的,在那裏self.lastNumber0.text
將與programtical self.lastNumber[d].text
<更換 - 我知道這是行不通的,但是這就是我想要實現的,我想這
NSString *labelName = [NSString stringWithFormat:@"lastNumber%i", d];
但無法弄清楚該怎麼辦self.labelName.text
=嗒嗒......
感謝您的閱讀,如果您需要任何更多的信息只是問。謝謝!
ANSWER
int yInc = 310;
for (int i = 0; i < 32; i++) {
label[i] = [[UILabel alloc] initWithFrame:CGRectMake(460, yInc, 100, 10)];
label[i].font = [UIFont boldSystemFontOfSize:11];
label[i].backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
[self.view addSubview:label[i]];
yInc = yInc - 10;
}
for (int d = 0; d < 32; d++) {
int value = [[lastNumbers objectAtIndex:d] intValue];
if ([red containsObject:[NSNumber numberWithInteger:value]])
{
label[d].text = [NSString stringWithFormat:@"%i", value];
label[d].textAlignment = NSTextAlignmentLeft;
label[d].textColor = [UIColor redColor];
}
if ([black containsObject:[NSNumber numberWithInteger:value]])
{
label[d].text = [NSString stringWithFormat:@"%i", value];
label[d].textAlignment = NSTextAlignmentRight;
label[d].textColor = [UIColor blackColor];
}
if ([green containsObject:[NSNumber numberWithInteger:value]])
{
label[d].text = [NSString stringWithFormat:@"%i", value];
label[d].textAlignment = NSTextAlignmentCenter;
label[d].textColor = [UIColor greenColor];
}
}
}
lastNumber23變量尖叫,說你的應用程序體系結構中存在嚴重錯誤... – Vladimir 2014-10-11 18:04:19
我試圖實現的目標我無法用一個標籤實現。所以爲了讓它爲小目的而工作,以及學習如何遍歷屬性名稱,現在是值得的。但感謝您的意見。 – Daniel 2014-10-11 18:08:42
您不能「遍歷屬性名稱」。 – matt 2014-10-11 18:17:25