有人能告诉我如何在我的方法中动态指定ivar名称吗? l2是我试图瞄准的伊娃。
//this works
if (maxunlocked > 1) {
filename = [NSString stringWithFormat:@"level%d.png", [[fliesArray objectAtIndex:2] intValue]];
filenameHi = [NSString stringWithFormat:@"level%dHi.png", [[fliesArray objectAtIndex:2] intValue]];
l2 = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level:)];
}
//这不是
for (int i = 0; i<11; i++) {
if (maxunlocked > i) {
filename = [NSString stringWithFormat:@"level%d.png", [[fliesArray objectAtIndex:i] intValue]];
filenameHi = [NSString stringWithFormat:@"level%dHi.png", [[fliesArray objectAtIndex:i] intValue]];
//this is where I'm attempting to dynamically specify the SoundMenuItem instance name.
sndMenuItem = [NSString stringWithFormat:@"l%d", i];
sndMenuItem = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level:)];
sndMenuItem.userData = (id)i;
}
}
谢谢,
标记
答案 0 :(得分:1)
如果您将其声明为属性,则可以使用KVC来获取它。
float h1 = [object height];
float h2 = [[object valueForKey:@"height"] floatValue];
[编辑]
我不明白你在说什么。答案是不。您无法动态指定变量名称。你能做的是:
// if `l2` is a member of self. (as in self.l2)
for (int i = 0; i<11; i++) {
if (maxunlocked > i) {
filename = [NSString stringWithFormat:@"level%d.png", [[fliesArray objectAtIndex:i] intValue]];
filenameHi = [NSString stringWithFormat:@"level%dHi.png", [[fliesArray objectAtIndex:i] intValue]];
//this is where I'm attempting to dynamically specify the SoundMenuItem instance name.
key = [NSString stringWithFormat:@"l%d", i];
tmp = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level:)];
tmp.userData = (id)i;
[self setValue:tmp forKey:key];
}
}
[编辑]
你应该重新构建整个班级。
@interface myViewController: NSViewController {
UIButton *sound1;
UIButton *sound2;
SoundMenuItem *l1;
SoundMenuItem *l2;
}
@property (assign) IBOutlet UIButton *sound1; // connect up in IB
@property (assign) IBOutlet UIButton *sound2;
- (IBAction) clickSoundButton: (id)sender; // connect up to sound1 and sound2 in IB
- (SoundMenuItem) getSoundMenuItem: (int) i;
@end
@implementation myViewController
- (IBAction) clickSoundButton: (id)sender
{
if (sender == (id)sound1) l1 = [self getSoundMenuItem: 1];
if (sender == (id)sound2) l2 = [self getSoundMenuItem: 2];
}
- (SoundMenuItem) getSoundMenuItem: (int) i
{
if (maxunlocked <= i) return
NSString *filename = [NSString stringWithFormat:@"level%d.png", [[fliesArray objectAtIndex:i] intValue]];
NSString *filenameHi = [NSString stringWithFormat:@"level%dHi.png", [[fliesArray objectAtIndex:i] intValue]];
SoundMenuItem *sndMenuItem = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level:)];
sndMenuItem.userData = (id)i;
return sndMenuItem; //(assuming it is auto-released)
}
@end
答案 1 :(得分:0)
问题澄清后,我的第一个答案无效。 @stephen给出了使用字典来保存所有SoundMenuItem的建议也是我的建议。
答案 2 :(得分:0)
对于缺乏信息感到抱歉,感谢您的耐心等待。由于字符限制,我很难将其全部添加到评论区域。
这就是我正在尝试的......
在我的标题文件中:
// SoundMenuItem是一个类 SoundMenuItem * l1; SoundMenuItem * l2; (我实际上有20个按钮,每个游戏级别一个)
在我的.m文件中
//here I set up the l1 button which is never locked
l1 = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level:)];
//and here I set the l2 button to be locked by default
l2 = [SoundMenuItem itemFromNormalSpriteFrameName:@"levellock.png" selectedSpriteFrameName:@"levellockHi.png" target:self selector:@selector(doNothing:)];
//现在我检查level2是否已解锁(maxunlocked&gt; 1),如果是,我重置l2实例以使用不同的图像。
if(maxunlocked&gt; 1){
filename = [NSString stringWithFormat:@“level%d.png”,[[fliesArray objectAtIndex:2] intValue]]; filenameHi = [NSString stringWithFormat:@“level%dHi.png”,[[fliesArray objectAtIndex:2] intValue]];
l2 = [SoundMenuItem itemFromNormalSpriteFrameName:filename selectedSpriteFrameName:filenameHi target:self selector:@selector(level :)];
}
因此,不是对上面的if语句进行20次迭代,而是每个按钮一次,我想将它重构为一个。
我希望自己更清楚。