我正在尝试设计一个界面,当用户单击复选框时,它将在NSView
框中显示图像。有 12 个复选框,我有……在我的脑海中……一个 if else 语句的布局来处理复选框的所有不同组合。我已经在声明中列出了 if else 语句-(void)drawRect:(NSRect)rect
,但它们似乎不起作用。(我没有收到错误)。我发现它确实在程序首次启动时正确评估了 if else 语句。但是,当我单击激活包含“ setNeedsDisplay:
”的操作的复选框时,不会重新评估“if else”语句。(这当然意味着我不明白究竟是如何setNeedsDisplay:
工作的。哈哈)
我NSLog
在不同的地方发表了一些声明,看看发生了什么。我在 -(void)drawRect 声明中有一个,以确保它setNeedsDisplay:
正在工作。确实如此。每当我单击一个复选框时,它都会激活 setNeedsDisplay。我在“if else”语句中的两个地方也放了一个NSLog
:一个在第一部分:
- (void)drawRect:(NSRect)rect {
if ([cNatural state] == NSOnState) {
NSLog(@"C Natural Selected");
NSPoint cNaturalPos;
cNaturalPos = NSMakePoint(35, 0);
NSImage * cNaturalImage;
cNaturalImage = [NSImage imageNamed:@"cNatural.png"];
[cNaturalImage dissolveToPoint:cNaturalPos fraction:(1.0)];}
else
if ([dFlat state] == NSOnState) {
NSPoint dFlatPos;
dFlatPos = NSMakePoint(35, 0);
NSImage * dFlatImage;
dFlatImage = [NSImage imageNamed:@"cSharp.png"];
[dFlatImage dissolveToPoint:dFlatPos fraction:(1.0)];}
else
......................
在第一组“if else”条件句的末尾有一个:
if ([bNatural state] == NSOnState) {
NSPoint bNaturalPos;
bNaturalPos = NSMakePoint(35, 0);
NSImage * bNaturalImage;
bNaturalImage = [NSImage imageNamed:@"bNatural.png"];
[bNaturalImage dissolveToPoint:bNaturalPos fraction:(1.0)];}
else {
NSLog(@"No First Note Selection.");
NSPoint emptyStaffPos;
emptyStaffPos = NSMakePoint(35, 0);
NSImage * emptyStaffImage;
emptyStaffImage = [NSImage imageNamed:@"emptyStaff.png"];
[emptyStaffImage dissolveToPoint:emptyStaffPos fraction:(1.0)];}
我希望当程序第一次加载时,“没有第一个音符选择”会出现在我的控制台中。它确实如此。没有发生的是,当我激活“cNatural”复选框时,它会在控制台中显示“No First Note Selection”字符串。就像它没有注册复选框的状态更改一样。
为了确保我在 Interface Builder 中正确连接了复选框,我创建了这行代码,当复选框被切换时激活:(此代码还包括setNeedsDisplay:
)
- (IBAction)rendStaffCanvas:(id)sender {
[staffDrawingCanvas setNeedsDisplay:YES];
if ([cNatural state] == NSOnState) {
NSLog(@"C Natural checkbox: ON");
}
else {
NSLog(@"C Natural checkbox: OFF");
}
if ([dFlat state] == NSOnState) {
NSLog(@"D Flat checkbox: ON");
}
else {
NSLog(@"D Flat checkbox: OFF");
}
if ([dNatural state] == NSOnState) {
NSLog(@"D Natural checkbox: ON");
}
所有这些都按照我在控制台中的预期显示,但-(void)drawRect
部分中的“if else”语句似乎没有注册。
我到处找,找不到解决办法。任何帮助都感激不尽。
这是我的 .m 文件的相当长的代码:
#import "StaffCanvas.h"
@implementation StaffCanvas
- (IBAction)rendStaffCanvas:(id)sender {
[staffDrawingCanvas setNeedsDisplay:YES];
if ([cNatural state] == NSOnState) {
NSLog(@"C Natural checkbox: ON");
}
else {
NSLog(@"C Natural checkbox: OFF");
}
if ([dFlat state] == NSOnState) {
NSLog(@"D Flat checkbox: ON");
}
else {
NSLog(@"D Flat checkbox: OFF");
}
if ([dNatural state] == NSOnState) {
NSLog(@"D Natural checkbox: ON");
}
else {
NSLog(@"D Natural checkbox: OFF");
}
if ([eFlat state] == NSOnState) {
NSLog(@"E Flat checkbox: ON");
}
else {
NSLog(@"E Flat checkbox: OFF");
}
if ([eNatural state] == NSOnState) {
NSLog(@"E Natural checkbox: ON");
}
else {
NSLog(@"E Natural checkbox: OFF");
}
if ([fNatural state] == NSOnState) {
NSLog(@"F Natural checkbox: ON");
}
else {
NSLog(@"F Natural checkbox: OFF");
}
if ([gFlat state] == NSOnState) {
NSLog(@"G Flat checkbox: ON");
}
else {
NSLog(@"G Flat checkbox: OFF");
}
if ([gNatural state] == NSOnState) {
NSLog(@"G Natural checkbox: ON");
}
else {
NSLog(@"G Natural checkbox: OFF");
}
if ([aFlat state] == NSOnState) {
NSLog(@"A Flat checkbox: ON");
}
else {
NSLog(@"A Flat checkbox: OFF");
}
if ([aNatural state] == NSOnState) {
NSLog(@"A Natural checkbox: ON");
}
else {
NSLog(@"A Natural checkbox: OFF");
}
if ([bFlat state] == NSOnState) {
NSLog(@"B Flat checkbox: ON");
}
else {
NSLog(@"B Flat checkbox: OFF");
}
if ([bNatural state] == NSOnState) {
NSLog(@"B Natural checkbox: ON");
}
else {
NSLog(@"B Natural checkbox: OFF");
}
}
- (void)drawRect:(NSRect)rect {
if ([cNatural state] == NSOnState) {
NSLog(@"C Natural Selected");
NSPoint cNaturalPos;
cNaturalPos = NSMakePoint(35, 0);
NSImage * cNaturalImage;
cNaturalImage = [NSImage imageNamed:@"cNatural.png"];
[cNaturalImage dissolveToPoint:cNaturalPos fraction:(1.0)];}
else
if ([dFlat state] == NSOnState) {
NSPoint dFlatPos;
dFlatPos = NSMakePoint(35, 0);
NSImage * dFlatImage;
dFlatImage = [NSImage imageNamed:@"cSharp.png"];
[dFlatImage dissolveToPoint:dFlatPos fraction:(1.0)];}
else
if ([dNatural state] == NSOnState) {
NSPoint dNaturalPos;
dNaturalPos = NSMakePoint(35, 0);
NSImage * dNaturalImage;
dNaturalImage = [NSImage imageNamed:@"dNatural.png"];
[dNaturalImage dissolveToPoint:dNaturalPos fraction:(1.0)];}
else
if ([eFlat state] == NSOnState) {
NSPoint eFlatPos;
eFlatPos = NSMakePoint(35, 0);
NSImage * eFlatImage;
eFlatImage = [NSImage imageNamed:@"eFlat.png"];
[eFlatImage dissolveToPoint:eFlatPos fraction:(1.0)];}
else
if ([eNatural state] == NSOnState) {
NSPoint eNaturalPos;
eNaturalPos = NSMakePoint(35, 0);
NSImage * eNaturalImage;
eNaturalImage = [NSImage imageNamed:@"eNatural.png"];
[eNaturalImage dissolveToPoint:eNaturalPos fraction:(1.0)];}
else
if ([fNatural state] == NSOnState) {
NSPoint fNaturalPos;
fNaturalPos = NSMakePoint(35, 0);
NSImage * fNaturalImage;
fNaturalImage = [NSImage imageNamed:@"fNatural.png"];
[fNaturalImage dissolveToPoint:fNaturalPos fraction:(1.0)];}
else
if ([gFlat state] == NSOnState) {
NSPoint fSharpPos;
fSharpPos = NSMakePoint(35, 0);
NSImage * fSharpImage;
fSharpImage = [NSImage imageNamed:@"fSharp.png"];
[fSharpImage dissolveToPoint:fSharpPos fraction:(1.0)];}
else
if ([gNatural state] == NSOnState) {
NSPoint gNaturalPos;
gNaturalPos = NSMakePoint(35, 0);
NSImage * gNaturalImage;
gNaturalImage = [NSImage imageNamed:@"gNatural.png"];
[gNaturalImage dissolveToPoint:gNaturalPos fraction:(1.0)];}
else
if ([aFlat state] == NSOnState) {
NSPoint aFlatPos;
aFlatPos = NSMakePoint(35, 0);
NSImage * aFlatImage;
aFlatImage = [NSImage imageNamed:@"aFlat.png"];
[aFlatImage dissolveToPoint:aFlatPos fraction:(1.0)];}
else
if ([aNatural state] == NSOnState) {
NSPoint aNaturalPos;
aNaturalPos = NSMakePoint(35, 0);
NSImage * aNaturalImage;
aNaturalImage = [NSImage imageNamed:@"aNatural.png"];
[aNaturalImage dissolveToPoint:aNaturalPos fraction:(1.0)];}
else
if ([bFlat state] == NSOnState) {
NSPoint bFlatPos;
bFlatPos = NSMakePoint(35, 0);
NSImage * bFlatImage;
bFlatImage = [NSImage imageNamed:@"bFlat.png"];
[bFlatImage dissolveToPoint:bFlatPos fraction:(1.0)];}
else
if ([bNatural state] == NSOnState) {
NSPoint bNaturalPos;
bNaturalPos = NSMakePoint(35, 0);
NSImage * bNaturalImage;
bNaturalImage = [NSImage imageNamed:@"bNatural.png"];
[bNaturalImage dissolveToPoint:bNaturalPos fraction:(1.0)];}
else {
NSLog(@"No First Note Selection.");
NSPoint emptyStaffPos;
emptyStaffPos = NSMakePoint(35, 0);
NSImage * emptyStaffImage;
emptyStaffImage = [NSImage imageNamed:@"emptyStaff.png"];
[emptyStaffImage dissolveToPoint:emptyStaffPos fraction:(1.0)];}
if ([dFlat state] == NSOnState) {
NSPoint dFlatPos;
dFlatPos = NSMakePoint(70, 0);
NSImage * dFlatImage;
dFlatImage = [NSImage imageNamed:@"cSharp.png"];
[dFlatImage dissolveToPoint:dFlatPos fraction:(1.0)];}
else
if ([dNatural state] == NSOnState) {
NSPoint dNaturalPos;
dNaturalPos = NSMakePoint(70, 0);
NSImage * dNaturalImage;
dNaturalImage = [NSImage imageNamed:@"dNatural.png"];
[dNaturalImage dissolveToPoint:dNaturalPos fraction:(1.0)];}
else
if ([eFlat state] == NSOnState) {
NSPoint eFlatPos;
eFlatPos = NSMakePoint(70, 0);
NSImage * eFlatImage;
eFlatImage = [NSImage imageNamed:@"eFlat.png"];
[eFlatImage dissolveToPoint:eFlatPos fraction:(1.0)];}
else
if ([eNatural state] == NSOnState) {
NSPoint eNaturalPos;
eNaturalPos = NSMakePoint(70, 0);
NSImage * eNaturalImage;
eNaturalImage = [NSImage imageNamed:@"eNatural.png"];
[eNaturalImage dissolveToPoint:eNaturalPos fraction:(1.0)];}
else
if ([fNatural state] == NSOnState) {
NSPoint fNaturalPos;
fNaturalPos = NSMakePoint(70, 0);
NSImage * fNaturalImage;
fNaturalImage = [NSImage imageNamed:@"fNatural.png"];
[fNaturalImage dissolveToPoint:fNaturalPos fraction:(1.0)];}
else
if ([gFlat state] == NSOnState) {
NSPoint fSharpPos;
fSharpPos = NSMakePoint(70, 0);
NSImage * fSharpImage;
fSharpImage = [NSImage imageNamed:@"fSharp.png"];
[fSharpImage dissolveToPoint:fSharpPos fraction:(1.0)];}
else
if ([gNatural state] == NSOnState) {
NSPoint gNaturalPos;
gNaturalPos = NSMakePoint(70, 0);
NSImage * gNaturalImage;
gNaturalImage = [NSImage imageNamed:@"gNatural.png"];
[gNaturalImage dissolveToPoint:gNaturalPos fraction:(1.0)];}
else
if ([aFlat state] == NSOnState) {
NSPoint aFlatPos;
aFlatPos = NSMakePoint(70, 0);
NSImage * aFlatImage;
aFlatImage = [NSImage imageNamed:@"aFlat.png"];
[aFlatImage dissolveToPoint:aFlatPos fraction:(1.0)];}
else
if ([aNatural state] == NSOnState) {
NSPoint aNaturalPos;
aNaturalPos = NSMakePoint(70, 0);
NSImage * aNaturalImage;
aNaturalImage = [NSImage imageNamed:@"aNatural.png"];
[aNaturalImage dissolveToPoint:aNaturalPos fraction:(1.0)];}
else
if ([bFlat state] == NSOnState) {
NSPoint bFlatPos;
bFlatPos = NSMakePoint(70, 0);
NSImage * bFlatImage;
bFlatImage = [NSImage imageNamed:@"bFlat.png"];
[bFlatImage dissolveToPoint:bFlatPos fraction:(1.0)];}
else
if ([bNatural state] == NSOnState) {
NSPoint bNaturalPos;
bNaturalPos = NSMakePoint(70, 0);
NSImage * bNaturalImage;
bNaturalImage = [NSImage imageNamed:@"bNatural.png"];
[bNaturalImage dissolveToPoint:bNaturalPos fraction:(1.0)];}
else {
NSPoint emptyStaffPos;
emptyStaffPos = NSMakePoint(70, 0);
NSImage * emptyStaffImage;
emptyStaffImage = [NSImage imageNamed:@"emptyStaff.png"];
[emptyStaffImage dissolveToPoint:emptyStaffPos fraction:(1.0)];}
if ([dNatural state] == NSOnState) {
NSPoint dNaturalPos;
dNaturalPos = NSMakePoint(105, 0);
NSImage * dNaturalImage;
dNaturalImage = [NSImage imageNamed:@"dNatural.png"];
[dNaturalImage dissolveToPoint:dNaturalPos fraction:(1.0)];}
else
if ([eFlat state] == NSOnState) {
NSPoint eFlatPos;
eFlatPos = NSMakePoint(105, 0);
NSImage * eFlatImage;
eFlatImage = [NSImage imageNamed:@"eFlat.png"];
[eFlatImage dissolveToPoint:eFlatPos fraction:(1.0)];}
else
if ([eNatural state] == NSOnState) {
NSPoint eNaturalPos;
eNaturalPos = NSMakePoint(105, 0);
NSImage * eNaturalImage;
eNaturalImage = [NSImage imageNamed:@"eNatural.png"];
[eNaturalImage dissolveToPoint:eNaturalPos fraction:(1.0)];}
else
if ([fNatural state] == NSOnState) {
NSPoint fNaturalPos;
fNaturalPos = NSMakePoint(105, 0);
NSImage * fNaturalImage;
fNaturalImage = [NSImage imageNamed:@"fNatural.png"];
[fNaturalImage dissolveToPoint:fNaturalPos fraction:(1.0)];}
else
if ([gFlat state] == NSOnState) {
NSPoint fSharpPos;
fSharpPos = NSMakePoint(105, 0);
NSImage * fSharpImage;
fSharpImage = [NSImage imageNamed:@"fSharp.png"];
[fSharpImage dissolveToPoint:fSharpPos fraction:(1.0)];}
else
if ([gNatural state] == NSOnState) {
NSPoint gNaturalPos;
gNaturalPos = NSMakePoint(105, 0);
NSImage * gNaturalImage;
gNaturalImage = [NSImage imageNamed:@"gNatural.png"];
[gNaturalImage dissolveToPoint:gNaturalPos fraction:(1.0)];}
else
if ([aFlat state] == NSOnState) {
NSPoint aFlatPos;
aFlatPos = NSMakePoint(105, 0);
NSImage * aFlatImage;
aFlatImage = [NSImage imageNamed:@"aFlat.png"];
[aFlatImage dissolveToPoint:aFlatPos fraction:(1.0)];}
else
if ([aNatural state] == NSOnState) {
NSPoint aNaturalPos;
aNaturalPos = NSMakePoint(105, 0);
NSImage * aNaturalImage;
aNaturalImage = [NSImage imageNamed:@"aNatural.png"];
[aNaturalImage dissolveToPoint:aNaturalPos fraction:(1.0)];}
else
if ([bFlat state] == NSOnState) {
NSPoint bFlatPos;
bFlatPos = NSMakePoint(105, 0);
NSImage * bFlatImage;
bFlatImage = [NSImage imageNamed:@"bFlat.png"];
[bFlatImage dissolveToPoint:bFlatPos fraction:(1.0)];}
else
if ([bNatural state] == NSOnState) {
NSPoint bNaturalPos;
bNaturalPos = NSMakePoint(105, 0);
NSImage * bNaturalImage;
bNaturalImage = [NSImage imageNamed:@"bNatural.png"];
[bNaturalImage dissolveToPoint:bNaturalPos fraction:(1.0)];}
else {
NSPoint emptyStaffPos;
emptyStaffPos = NSMakePoint(105, 0);
NSImage * emptyStaffImage;
emptyStaffImage = [NSImage imageNamed:@"emptyStaff.png"];
[emptyStaffImage dissolveToPoint:emptyStaffPos fraction:(1.0)];}
NSLog(@"Redisplayed.");
NSPoint clefPos;
clefPos = NSMakePoint(0, 0);
NSImage * clefImage;
clefImage = [NSImage imageNamed:@"clefStart.png"];
[clefImage dissolveToPoint:clefPos fraction:(1.0)];
}
@end