我很难过。我没有任何错误甚至警告。我在调用它的行上设置了一个断点,程序在那里停止,但是当我在该选择器的实际代码中设置断点时,程序不会停止。我关心的选择器是toggleCellAtX:Y:
。这里的SPPuzzle.m:为什么我的Cocoa选择器实际上不被调用?
#import "SPPuzzle.h"
@implementation SPPuzzle
- (id)init
{
self = [super init];
if (self) {
int x, y;
for (y=0; y<3; y++)
for (x=0; x<3; x++) {
cells[x][y] = FALSE;
}
}
return self;
}
- (BOOL)cellOnAtX:(int)x Y:(int)y {
return cells[x][y];
}
- (void)toggleCellAtX:(int)x Y:(int)y {
cells[x][y] = !cells[x][y]; // Breakpoint here doesn't stop program.
}
@end
而这里的SPPuzzleView.m:
#import "SPPuzzleView.h"
@implementation SPPuzzleView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
puzzleInstance = [[SPPuzzle alloc] init];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
int x, y;
for (y=0; y<3; y++)
for (x=0; x<3; x++) {
if ([puzzleInstance cellOnAtX:x Y:y]) {
[[NSColor greenColor] set];
} else {
[[NSColor redColor] set];
}
[[NSBezierPath bezierPathWithRect:NSMakeRect(100*x, 100*y, 100, 100)] fill];
[[NSColor blackColor] set];
[[NSBezierPath bezierPathWithRect:NSMakeRect(100*x, 100*y, 100, 100)] stroke];
}
}
- (void)mouseDown:(NSEvent *)theEvent {
int x = (int)[theEvent locationInWindow].x/100;
int y = (int)[theEvent locationInWindow].y/100;
[puzzleInstance toggleCellAtX:x Y:y]; // Breakpoint here does stop program.
[self setNeedsDisplay:TRUE];
}
@end
我添加了注释,以显示在断点。我究竟做错了什么?
顺便说一下,我在调用方法时使用了Step Into,但它只是转到下一行([self setNeedsDisplay:TRUE];
)。
验证'puzzleInstance'不是'nil'。如果你的视图是从xib实例化的,这很可能发生,因为IIRC它调用'initWithCoder:'然后'awakeFromNib'而不是'initWithFrame:'。这是一个长镜头,但如果它有效,请告诉我,我会重新发布它作为答案。 – zneak 2012-02-07 03:26:19
@zneak这不是一个远射,它几乎肯定是问题所在。 – dasblinkenlight 2012-02-07 03:31:56
是的,它没什么好,我认为这是监视列表中的0x0。得到它了。谢谢! – flarn2006 2012-02-07 03:43:15