PDA

View Full Version : [Resolved] Trying to subclass NSView




Hans Kamp
Mar 25, 2013, 01:14 PM
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:
//
// 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:

//
// 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?



ArtOfWarfare
Mar 25, 2013, 02:19 PM
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
Mar 25, 2013, 02:19 PM
Have you placed a custom object onto your Window in Interface Builder and assigned that class to it?

http://i.imgur.com/ujQPTr6l.png

http://i.imgur.com/Utp3nEyl.png

Hans Kamp
Mar 25, 2013, 02:30 PM
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.

Hans Kamp
Mar 25, 2013, 02:59 PM
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
Mar 25, 2013, 03:17 PM
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.)

Hans Kamp
Mar 25, 2013, 03:24 PM
Here it is.

Thanks in advance for looking into it.

Ap0ks
Mar 25, 2013, 04:07 PM
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.

Hans Kamp
Mar 25, 2013, 04:16 PM
Thanks, I will try that again.

Hans Kamp
Mar 25, 2013, 04:43 PM
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.