【发布时间】:2012-01-24 14:08:04
【问题描述】:
请原谅,这里的新手。
我有一个动态构建的 NSString 值,它是 UILabel 实例的名称。我想向标签发送消息以更新其文本。但是,这两种数据类型不匹配。这里有足够的代码(我认为):
在头文件中:
IBOutlet UILabel *Clue1; // IBOutlet and IBAction are IDE flags
IBOutlet UILabel *Clue2; // IB = interface builder
IBOutlet UILabel *Clue3;
在实现文件中:
- (IBAction) newPuzzle:(id)sender { // Clear all fields & get new clue
[Clue1 setText:@""]; // Clear the fields
[Clue2 setText:@""];
[Clue3 setText:@""];
// Send up a randomly chosen new clue
NSArray *clues = [NSArray arrayWithObjects:@"222", @"333", nil];
NSInteger randomIndex = arc4random()%[clues count];
NSString *aClue = [clues objectAtIndex:randomIndex];
// The clue will be split into component digits and each piece sent to a different label
for (NSInteger charIdx = 0; charIdx < aClue.length; charIdx++) {
NSString *cluePos = [NSString stringWithFormat:@"Clue%d", charIdx + 1];
NSLog(@"%@", cluePos); // works
[cluePos setText:@"test"]; // Xcode notes the type mismatch
}
}
关于 SO 有一些类似的问题,但没有一个足够接近让我认识到它们适用于我的案例,至少据我所知。使用另一种语言 (R) 的术语,我需要“强制”从 NSString 到 UILabel 的线索位置类。我在 Xcode 4.2.1 和 OSX 10.7.2 上。
TIA。
【问题讨论】:
标签: objective-c ios