解决一个问题;编号为;移动滑块时的变量,Objective-C
在我的MacOSX项目中,我有一个滑块和两个“编号”NSImageView插座。根据滑块的值,我希望为进一步的操作处理相应的NSImageView 类似这样的内容:解决一个问题;编号为;移动滑块时的变量,Objective-C,objective-c,Objective C,在我的MacOSX项目中,我有一个滑块和两个“编号”NSImageView插座。根据滑块的值,我希望为进一步的操作处理相应的NSImageView 类似这样的内容: sliderRed value == 1 -> red1 sliderRed value == 2 -> red2 sliderRed value == 3 -> red3 sliderRed value == 4 -> red4 ... and so forth 目前我不知道如何做到这一点,也
sliderRed value == 1 -> red1
sliderRed value == 2 -> red2
sliderRed value == 3 -> red3
sliderRed value == 4 -> red4
... and so forth
目前我不知道如何做到这一点,也不想使用用例构造
有什么办法吗
@property (strong) IBOutlet NSSlider *sliderRed;
@property (strong) IBOutlet NSImageView *red1;
@property (strong) IBOutlet NSImageView *red2;
@property (strong) IBOutlet NSImageView *red3;
@property (strong) IBOutlet NSImageView *red4;
@property (strong) IBOutlet NSImageView *red5;
@property (strong) IBOutlet NSImageView *red6;
@property (strong) IBOutlet NSImageView *red7;
@property (strong) IBOutlet NSImageView *red8;
@property (strong) IBOutlet NSImageView *red9;
@property (strong) IBOutlet NSImageView *red10;
@property (strong) IBOutlet NSImageView *red11;
@property (strong) IBOutlet NSImageView *red12;
@property (strong) IBOutlet NSImageView *red13;
@property (strong) IBOutlet NSImageView *red14;
@property (strong) IBOutlet NSImageView *red15;
您可以向所有NSImageView添加标记 现在将这些NSImageView实例保存在一个数组中
NSMutableArray *arrayOfImageViews = [NSMutableArray array];
[arrayOfImageViews addObject:red1];
[arrayOfImageViews addObject:red2];
.
.
.
[arrayOfImageViews addObject:red15];
完成后,可以在特定滑块值上循环数组元素
NSImageView *selectedImageView = nil;
//Lets say your slider value is 7 for now.
for(NSImageView *imageView in arrayOfImageViews)
{
if(imageView.tag==mySlider.value)
{
selectedImageView = imageView;
break;
}
}
此循环结束后,您将在selectedImageView中查看图像。您现在可以使用它来完成任务。您可以使用键值编码:
NSString *key = [NSString stringWithFormat:@"red%d", sliderValue];
NSImageView *imageView = [self valueForKey:key];
缺点是编译器无法检查密钥的有效性。如果sliderValue
超出范围1。。15(在您的情况下)将出现运行时异常。谢谢。这就是我到目前为止想到的。但我想到了这样的事情。首先将正确的变量创建为string:NSString*pointerText=[NSString stringWithFormat:@“\u red%i”,[\u sliderRed intValue];如果[\u sliderRed intValue]等于1点,则文本将为“\u red1”。在4D中,可以创建指向具有该名称的变量的指针。我希望这是可以理解的。不,我不明白这里的4D是什么。你能解释一下吗?另外,我认为即使你应用了一个格式化的键,你仍然需要一些数组或集合来保存nsimageviews来进行迭代。这里的有效性不是问题。它永远不会超出范围。非常感谢这个伟大的提示。嗯,标签检查比键(字符串)检查更快。这个解决方案很好,但我的一个问题是,如何迭代所有nsimageview?您不需要一个数组或集合来容纳所有这些nsimageview吗?这个项目非常简单。红色图像视图是否被隐藏。这取决于滑块。我只需要一个指向当前红色的指针。这个解决方案非常适合我的需要。
NSString *key = [NSString stringWithFormat:@"red%d", sliderValue];
NSImageView *imageView = [self valueForKey:key];