0

谁能告诉我如何在我的方法中动态指定一个 ivar 名称?l2 是我要定位的 ivar。

//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;
     }
    }

谢谢,

标记

4

3 回答 3

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
于 2010-11-23T14:06:16.753 回答
0

澄清问题后,我的第一个答案无效。@stephen 提出的使用字典来保留所有 SoundMenuItem 的建议的答案也是我所提议的。

于 2010-11-23T14:50:46.237 回答
0

抱歉信息不足,感谢您的耐心等待。由于字数限制,我一直在努力将其全部添加到评论区。

这就是我正在尝试的...

在我的头文件中:

//SoundMenuItem 是一个类 SoundMenuItem *l1; 声音菜单项 *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 > 1),如果是,我重置 l2 实例以使用不同的图像。

if (maxunlocked > 1) {

文件名 = [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 次迭代,而是为每个按钮一个。

我希望我已经让自己更清楚了。

于 2010-11-23T15:06:41.203 回答