 |
 |
Weird NSInvalidArgumentException
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Feb 2007
Status:
Offline
|
|
I'm getting this error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[CALayerArray drawInContext:]: unrecognized selector sent to instance 0xd1b2f0'
only when I call a method of the class TBLevel in the drawRect: method of TBView.
Here's the code:
TBLevel.h
Code:
#import <Foundation/Foundation.h>
#ifndef _TB_OBJ
#define _TB_OBJ
typedef struct
{
CGImageRef image;
BOOL isObject;
} TBObject;
#endif
@interface TBLevel : NSObject
{
int _width;
int _height;
int _playerX;
int _playerY;
TBObject **cells;
CGContextRef context;
}
+ (id)levelWithWidth:(int)width height:(int)height playerX:(int)playerX andPlayerY:(int)playerY;
- (id)initWithWidth:(int)width height:(int)height playerX:(int)playerX andPlayerY:(int)playerY;
- (void)insertObject:(TBObject)obj atPoint:(CGPoint)loc;
- (void)drawInContext:(CGContextRef)ctxt;
- (int)width;
- (int)height;
@end
TBlevel.m
Code:
#import "TBLevel.h"
@implementation TBLevel
+ (id)levelWithWidth:(int)width height:(int)height playerX:(int)playerX andPlayerY:(int)playerY
{
return [[[TBLevel alloc] initWithWidth:width height:height playerX:playerX andPlayerY:playerY] autorelease];
}
- (id)initWithWidth:(int)width height:(int)height playerX:(int)playerX andPlayerY:(int)playerY
{
_width = width;
_height = height;
_playerX = playerX;
_playerY = playerY;
cells = (TBObject **)malloc(sizeof(TBObject *) * width);
for (int i = 0; i < width; i++)
{
cells[i] = (TBObject *)malloc(sizeof(TBObject) * height);
}
return self;
}
- (void)drawInContext:(CGContextRef)ctxt
{
context = ctxt;
for (int i = 0; i < _width; i++)
{
for (int j = 0; j < _height; j++)
{
CGImageRef image = cells[i][j].image;
CGRect imgRect = CGRectMake(i * 32, j * 32, 32, 32);
CGContextDrawImage(context, imgRect, image);
CGImageRelease(image);
}
}
}
- (void)insertObject:(TBObject)obj atPoint:(CGPoint)loc
{
cells[(int)loc.x][(int)loc.y] = obj;
}
- (int)width
{
return _width;
}
- (int)height
{
return _height;
}
- (void)dealloc
{
free(cells);
cells = NULL;
CGContextRelease(context);
[super dealloc];
}
@end
TBView.m
Code:
#import "TBView.h"
@implementation TBView
- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super initWithCoder:coder])
{
level = [TBLevel levelWithWidth:10 height:10 playerX:2 andPlayerY:1];
TBObject obj;
obj.image = [UIImage imageNamed:@"test.png"].CGImage;
obj.isObject = YES;
[level insertObject:obj atPoint:CGPointMake(0, 1)];
//Notice the above method call (it doesn't generate an exception)
playerView = [[TBPlayerView alloc] initWithFrame:CGRectMake(144, 144, 32, 32)];
tallObjectsView = [[TBTallObjectsView alloc] initWithFrame:self.frame];
[self addSubview:playerView];
[self addSubview:tallObjectsView];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[[UIColor blackColor] set];
UIRectFill(rect);
[level drawInContext:UIGraphicsGetCurrentContext()]; //This is the method call that generates the exception, if I put [level drawInContext:NULL] in the initWithCoder: method, it works
[playerView setNeedsDisplay];
[tallObjectsView setNeedsDisplay];
}
- (void)dealloc
{
[super dealloc];
}
@end
Notice the comments in TBView.m.
If I call a method of level from initWithCoder: nohthing happens, but if I call it from drawRect: I get an exception.
Am I missing something?
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |