Resolved Trying to subclass NSView

Discussion in 'Mac Programming' started by Hans Kamp, Mar 25, 2013.

  1. Hans Kamp, Mar 25, 2013
    Last edited: Mar 26, 2013

    macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #1
    I am trying to subclass NSView but to no avail.

    I have an empty window with a simple menu (which I will care about it later) and then I try to subclass NSView. To do that, I click on: File, New, File... I click on Objective-C class and then click on Next. After "Class" I type: DrawingView. After "Subclass of" I type: NSView. Two new files DrawingView.h and DrawingView.m are created.

    I update the files as follows:

    DrawingView.h:
    Code:
    //
    //  DrawingView.h
    //  Drawing
    //
    //  Created by Hans Kamp on 25-03-13.
    //  Copyright (c) 2013 Hans Kamp. All rights reserved.
    //
    
    #import <Cocoa/Cocoa.h>
    
    @interface DrawingView : NSView
    - (void) drawRect:(NSRect)frame;
    @end
    
    DrawingView.m:

    Code:
    //
    //  DrawingView.m
    //  Drawing
    //
    //  Created by Hans Kamp on 25-03-13.
    //  Copyright (c) 2013 Hans Kamp. All rights reserved.
    //
    
    #import "DrawingView.h"
    
    @implementation DrawingView
    - (void) drawRect:(NSRect)frame {
        [[NSColor redColor] set];
        [NSBezierPath fillRect:frame];
    }
    @end
    
    But the method drawRect is not called. What am I missing?
     
  2. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Do you have an instance of your DrawingView created and displayed anywhere? IE, have you added an instance of your DrawingView to your window in the interface XIB or have you added some code to your controller to create an instance and add it to the view hierarchy?
     
  3. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #3
    Have you placed a custom object onto your Window in Interface Builder and assigned that class to it?

    [​IMG]

    [​IMG]
     
  4. thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #4
    No, I haven't, but I am still struggling with it. But... thanks for the info. I will let you know whether I have more problems.
     
  5. thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #5
    Okay, I have added a Custom View, and changed its class to DrawingView.

    From DrawingView only initWithFrame is called. I didn't get a message from setFrameSize, nor from drawRect.

    Are the settings from the Custom View wrong?
     
  6. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    Share your code or zip up your project and share that (I don't like making that suggestion but it sounds like your issue may be with your XIB and not your code.)
     
  7. thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #7
    Here it is.

    Thanks in advance for looking into it.
     

    Attached Files:

  8. macrumors regular

    Joined:
    Aug 12, 2008
    Location:
    Cambridge, UK
    #8
    Works fine with one small change, Select MainMenu.xib from the Project Navigator, then the window in the xib editor (if it's not already shown). Click the window so it's selected then in the Identity Inspector change the class from NSView to Drawing.

    Run and you'll see your Red view.

    This is what robvas pointed out earlier, if you check his first image, it's the custom class > SmileyView bit you're missing.
     
  9. thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #9
    Thanks, I will try that again.
     
  10. thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #10
    And now it is working. It wasn't clear what Robvas had selected in the screenshots.

    From there I can do more experiments with drawing.
     
  11. macrumors newbie

    Joined:
    Oct 21, 2012
    #11
    Thanks for asking this question. Now I too know..


    This is a most interesting question, and thank you for uploading the zip code file. Now I too understand perfectly how this works. I have been wracking my brains trying to make a similar thing work here.
    I think I will repeat the operation many times so that I remember !!
    .. because what I want to do is actually draw some gauges and graphs to display basic weather station data.
     

Share This Page