title: Objective-C: Using "if else" statements inside -(void)drawRect to get a checkbox state?
body: <p>I am trying to design an interface where when the user clicks a checkbox, it will display an image in a <code>NSView</code> box. There are 12 checkboxes, and I have ... in my mind... a layout of if else statements to handle all the different combinations of checked boxes. I have laid out the if else statements inside the <code>-(void)drawRect:(NSRect)rect</code> declaration, but they do not seem to be working. (I am not getting errors). I have figured out that it does evaluate the if else statements correctly when the program is first initiated. But when I click the checkbox that activates an action containing a "<code>setNeedsDisplay:</code>", the "if else" statements are not reevaluated. (Which of course means I don't understand exactly how <code>setNeedsDisplay:</code> works.lol) </p>
  
  <p>I put a couple <code>NSLog</code> statements in various places to see what was happening. I have one in the -(void)drawRect declaration to make sure that the <code>setNeedsDisplay:</code> was working. And it was. Whenever I click a checkbox, it activates setNeedsDisplay.
  I put a <code>NSLog</code> also in two places in my "if else" statement:
  One at the first part: </p>
  
  <pre><code>- (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
            ......................
  </code></pre>
  
  <p>And one at the end of the first set of "if else" conditionals:</p>
  
  <pre><code>        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)];}
  </code></pre>
  
  <p>I would expect that when the program was first loaded, the "No First Note Selection" would appear in my console. And it does. What doesn't happen is, when I activate the "cNatural" checkbox, it instead displays the "No First Note Selection" string in the console. Its like it is not registering the state change of the checkbox. </p>
  
  <p>To make sure I had the checkboxes properly connected in Interface Builder, I created this line of code that activates when the checkbox is toggled: (This code also includes the <code>setNeedsDisplay:</code>)</p>
  
  <pre><code>- (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");
  }
  </code></pre>
  
  <p>All of this displays as I would expect in the console, but the "if else" statements in the <code>-(void)drawRect</code> part just don't seem to be registering.</p>
  
  <p>I have looked everywhere and can't find a solution to this. Any help will be much appreciated.</p>
  
  <p>Here is the rather lengthy code for my .m file:</p>
  
  <pre><code>#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
  </code></pre>