Я пытаюсь разработать интерфейс, в котором, когда пользователь нажимает на флажок, он отображает изображение в поле NSView
.Есть 12 флажков, и у меня есть ... в моем представлении ... макет операторов if else для обработки всех различных комбинаций флажков.Я поместил оператор if else в декларации -(void)drawRect:(NSRect)rect
, но они, похоже, не работают.(Я не получаю ошибки).Я понял, что он правильно оценивает операторы if else при первом запуске программы.Но когда я нажимаю флажок, который активирует действие, содержащее «setNeedsDisplay:
», операторы «if else» не переоцениваются.(Что, конечно, означает, что я не совсем понимаю, как работает setNeedsDisplay:
..lol)
Я поместил пару NSLog
утверждений в разных местах, чтобы увидеть, что происходит.У меня есть один в объявлении - (void) drawRect, чтобы убедиться, что setNeedsDisplay:
работает.И это было.Всякий раз, когда я нажимаю флажок, он активирует setNeedsDisplay.Я поставил 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 ([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)];}
Я ожидаю, что при первой загрузке программы в моей консоли появится сообщение «No First Note Selection».И это так.Чего не происходит, когда я активирую флажок «cNatural», вместо этого он отображает строку «No First Note Selection» в консоли.Как будто он не регистрирует изменение состояния флажка.
Чтобы убедиться, что я правильно установил флажки в Интерфейсном Разработчике, я создал эту строку кода, которая активируется при переключении флажка: (Этот код также включает 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
, похоже, просто не регистрируются.
Я искал везде и не могунайти решение для этого.Любая помощь будет высоко ценится.
Вот довольно длинный код для моего файла .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