1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

initWithFrame problems

Discussion in 'Mac Programming' started by eddjc, Feb 9, 2008.

  1. macrumors newbie

    #1
    Hey all,

    I'm having problem with NSView's initWithFrame class - it's the 'Dot View' app from "Learning Cocoa"'s examples - here's the source:

    Code:
    #import "DotView.h"
    
    
    @implementation DotView
    
    - (id)initWIthFrame:(NSRect)frame
    {
    	self = [super initWithFrame:frame];
    	center.x = 50.0;
    	center.y = 50.0;
    	radius = 20.0;
    	color = [[NSColor redColor] retain];
    	return self;
    }
    
    - (void)awakeFromNib
    {
    
    // this line added to make it work	
    color = [[NSColor redColor] retain];
    
    	[colorWell setColor:color];
    	[slider setFloatValue:radius];
    }
    
    - (void)dealloc
    {
    	[color release];
    	[super dealloc];
    }
    
    - (void)drawRect:(NSRect)rect
    {
    	NSRect dotRect;
    	
    	[[NSColor whiteColor] set];
    	NSRectFill([self bounds]);
    	
    	dotRect.origin.x = center.x - radius;
    	dotRect.origin.y = center.y - radius;
    	
    	dotRect.size.width = 2 * radius;
    	dotRect.size.height = 2 * radius;
    	
    	[color set];
    	
    	[[NSBezierPath bezierPathWithOvalInRect:dotRect] fill];
    }
    
    - (BOOL)isOpaque
    {
    	return YES;
    }
    
    - (void)mouseDown:(NSEvent *)event
    {
    	NSPoint eventLocation = [event locationInWindow];
    	center = [self convertPoint:eventLocation fromView:nil];
    	[self setNeedsDisplay:YES];
    }
    
    - (IBAction)setColor:(id)sender {
        NSColor * newColor = [sender color];
    	[newColor retain];
    	[color release];
    	color = newColor;
    	[self setNeedsDisplay:YES];
    }
    
    - (IBAction)setRadius:(id)sender {
        radius = [sender floatValue];
    	[self setNeedsDisplay:YES];
    }
    @end
    When I first built this app it threw back a *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: aColor != nil'

    Then I added the line indicated above to make sure there was a value in colorWell and it built fine - the trouble being it wasn't initialized with the values in initWithFrame (Radius was set to 0) so it looks to me like either initWithFrame isn't loading, or awakeFromNib is loading first.

    Any ideas?

    Thanks

    Edd
     
  2. Moderator emeritus

    kainjow

    #2
    initWIthFrame should be initWithFrame

    Objective-C is case sensitive.
     
  3. macrumors 65816

    deputy_doofy

    #3
    Just to clarify...
     

Share This Page