Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 25, 2013, 01:14 PM   #1
Hans Kamp
macrumors member
 
Join Date: Mar 2013
Location: Enschede, Netherlands
Trying to subclass NSView

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 by Hans Kamp; Mar 26, 2013 at 11:25 AM.
Hans Kamp is offline   0 Reply With Quote
Old Mar 25, 2013, 02:19 PM   #2
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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?
ArtOfWarfare is offline   1 Reply With Quote
Old Mar 25, 2013, 02:19 PM   #3
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: USA
Have you placed a custom object onto your Window in Interface Builder and assigned that class to it?



robvas is offline   2 Reply With Quote
Old Mar 25, 2013, 02:30 PM   #4
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: Enschede, Netherlands
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 is offline   0 Reply With Quote
Old Mar 25, 2013, 02:59 PM   #5
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: 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?
Hans Kamp is offline   0 Reply With Quote
Old Mar 25, 2013, 03:17 PM   #6
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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.)
ArtOfWarfare is offline   1 Reply With Quote
Old Mar 25, 2013, 03:24 PM   #7
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: Enschede, Netherlands
Here it is.

Thanks in advance for looking into it.
Attached Files
File Type: zip Drawing.zip (27.8 KB, 22 views)
Hans Kamp is offline   0 Reply With Quote
Old Mar 25, 2013, 04:07 PM   #8
Ap0ks
macrumors regular
 
Join Date: Aug 2008
Location: Cambridge, UK
Quote:
Originally Posted by Hans Kamp View Post
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.
Ap0ks is offline   2 Reply With Quote
Old Mar 25, 2013, 04:16 PM   #9
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: Enschede, Netherlands
Thanks, I will try that again.
Hans Kamp is offline   0 Reply With Quote
Old Mar 25, 2013, 04:43 PM   #10
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: Enschede, Netherlands
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.
Hans Kamp is offline   0 Reply With Quote
Old Aug 31, 2014, 09:39 AM   #11
rachalmers
macrumors newbie
 
Join Date: Oct 2012
Thanks for asking this question. Now I too know..

Quote:
Originally Posted by Hans Kamp View Post
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.
rachalmers is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Add elements to View from subclass Pades iPhone/iPad Programming 1 May 9, 2014 06:51 AM
Subclassing a subclass - question regarding IBOutlets and IBActions moonman239 iPhone/iPad Programming 1 Sep 27, 2013 08:15 PM
Custom NSView with subview of NSSplitView ArtOfWarfare Mac Programming 3 Aug 20, 2013 05:16 AM
Trying to subclass UIView Hans Kamp iPhone/iPad Programming 3 Mar 25, 2013 12:15 AM
A single instance of a UIView subclass across the cells of UICollectionView naphatkrit iPhone/iPad Programming 3 Feb 1, 2013 02:17 PM

Forum Jump

All times are GMT -5. The time now is 07:13 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC