1

我正在尝试设计一个界面,当用户单击复选框时,它将在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
4

2 回答 2

6

当您将视图视为模型时,这不是解决此问题的特别好方法。如果您还没有阅读模型视图控制器 (MVC) 模式,请阅读。

我将有一个布尔值 NSUserDefault 每个开关或一个 obj-c 属性,具体取决于您希望它的持久性。

所以用你的开关来制作你的 XIB,然后将开关的值绑定到你的属性(它在检查器中从左数第四个带有两个绿色斑点)。

    @class FooController : NSObject
    {

    BOOL switch1;
    BOOL switch2;

    }

@property (readwrite,assign) BOOL switch1;
@property (readwrite,assign) BOOL switch2;

然后假设这是 NIB 的所有者对象

@class FooController

@synthesise switch1,switch2;

-(void)awakeFromNib
{

self.switch1 = YES; //switch1 starts on 
self.switch2 = NO; //switch2 starts off


[self addObserver:self forKeyPath:@"switch1" options:0 context:NULL];
[self addObserver:self forKeyPath:@"switch2" options:0 context:NULL];


}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                    change:(NSDictionary *)change
                       context:(void *)context
{
if([keyPath isEqualToString:@"switch1"]) { //do things related to switch1 }
else if ([keyPath isEqualToString:@"switch2"]) { //do things related to switch2 }
etc...
}

@end

自我观察自我的东西看起来有点奇怪,但这意味着当绑定到相应属性的开关在界面中发生更改时,您就会了解它。而且您没有编写大量的 get/set 方法。

NSUserDefaults 对象同样可通过此方法观察到

[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self 
                                                          forKeyPath:@"switch1" 
                                                             options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) 
                                                             context:NULL];

对模型值的任何更改都会反映在视图中,并且视图中的任何更改都会记录在观察协议中

查看此文档http://developer.apple.com/library/mac/documentation/cocoa/conceptual/KeyValueObserving/

于 2011-02-14T22:58:35.193 回答
5

除了让您的复选框反映您的控制器拥有的布尔值,而不是自己拥有这些值,并将此逻辑移至控制器之外,您还应该刻录大部分复制和粘贴的代码。

看起来您对每种情况都在做同样的事情,只是真正改变了图像,所以对所有情况使用相同的position变量(具有相同的值)和相同的image变量。

取消if陈述。在 IB 中设置复选框的标签,并在控制器的复选框操作方法中,使用发送者的标签来确定要显示的图像。(您是否使用数组或字典执行此操作取决于您,但您必须提前设置它,例如 in init,无论哪种方式。)

您可能想要制作一个包含图像名称数组或字典的 plist 文件,并将其加载到您的init方法中。您可以使用另一种方法尝试从带有标签号作为键的字典中检索 NSImage,如果标签不在该字典中,则使用 tags-to-names 字典(来自 plist 文件)来获取图像名称,使用该名称加载图像,并将图像存储在 tags-to-images 字典中以供将来检索。您将在单击复选框时调用此方法,传递复选框的标签,并将返回的图像传递给您的自定义视图以显示。

如果您的目标是 Sn​​ow Leopard,则可以使用NSCache而不是 NSMutableDictionary 进行标签到图像的映射。这使您可以设置一次保持加载多少图像的限制。

顺便说一句,dissolveToPoint:fraction:自雪豹以来已被弃用。你应该drawAtPoint:fromRect:operation:fraction:改用。传递NSZeroRect源矩形以使用整个图像。

此外,如果您确实意味着用户一次只能选择一张图像,您应该使用单选按钮,而不是复选框。将其实现为包含 NSButtonCells 的 NSMatrix;最简单的方法是创建一个单选按钮,然后在 IB 中选择调整它的大小。单元格是您应该设置的标签和操作。只有当用户可以选择要以某种方式组合的多个图像(例如,将它们组合在一起)时,复选框才是合适的。

于 2011-02-15T01:03:12.773 回答