Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Hans Kamp

macrumors member
Original poster
Mar 24, 2013
38
0
Enschede, Netherlands
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?
 
Last edited:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,561
6,059
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?
 

robvas

macrumors 68040
Mar 29, 2009
3,240
629
USA
Have you placed a custom object onto your Window in Interface Builder and assigned that class to it?

ujQPTr6l.png


Utp3nEyl.png
 

Hans Kamp

macrumors member
Original poster
Mar 24, 2013
38
0
Enschede, Netherlands
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?
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,561
6,059
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.)
 

Ap0ks

macrumors 6502
Aug 12, 2008
316
93
Cambridge, UK
Here it is.

Thanks in advance for looking into it.
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.
 

rachalmers

macrumors member
Oct 21, 2012
57
5
UK
Thanks for asking this question. Now I too know..

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.


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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.