Objective c drawRect查看空数组
drawtestViewController.m中的按钮链接到ChalkBoard.m中的按钮操作。它用CGpoint对象填充pointArray。除了从drawRect查看外,此数组到处都是对象。从drawRect看来,我不知何故引用了原始数组的空副本,但我找不到任何错误 我已经上传了整个项目在Objective c drawRect查看空数组,objective-c,ios,nsmutablearray,drawrect,Objective C,Ios,Nsmutablearray,Drawrect,drawtestViewController.m中的按钮链接到ChalkBoard.m中的按钮操作。它用CGpoint对象填充pointArray。除了从drawRect查看外,此数组到处都是对象。从drawRect看来,我不知何故引用了原始数组的空副本,但我找不到任何错误 我已经上传了整个项目在 drawtestViewController.m中的按钮操作 - (IBAction)myDo { NSLog(@"-Button was pressed"); [self.boa
drawtestViewController.m中的按钮操作
- (IBAction)myDo {
NSLog(@"-Button was pressed");
[self.board pushPoint:CGPointMake(100, 100 )];
[self.board pushPoint:CGPointMake(10, 100 )];
[self.graphicView setNeedsDisplay];
}
将
drawRect
方法中的pointArray
替换为self.pointArray
。
使用pointArray
时,变量按原样使用(不调用getter方法)。使用self.pointArray
时,将使用getter方法
我还建议您将getter和setter留给编译器在init方法中生成和初始化pointArray。问题实际上不在
黑板类中,而是在drawtestViewcontroller中。您的myDo
操作将点添加到self.board
,但会将setNeedsDisplay
消息发送到self.graphicsview
,后者也是黑板,但与您添加点的黑板不同
因此,您看到的第二条日志消息实际上来自一个完全不同的对象。添加了self,但没有更改-(void)drawRect:(CGRect)rect{NSLog(@)-drawRect已执行,大小为%u,数组:%@,[self.pointArray count],self.pointArray);}只是为了让您高兴,你的回答让我在看了10小时我的观点后省去了麻烦:)祝你周末愉快!可能是你无意中有两个不同的黑板对象吗?一个在窗口中以便绘制,另一个连接到板
插座,从而将点推送到它。尝试在两种方法中使用“%p”记录self
。我找不到类似的内容:(我已将整个项目上载到
// ChalkBoard.h
#import <UIKit/UIKit.h>
@interface ChalkBoard : UIView
-(void)pushPoint:(CGPoint)point;
@property (nonatomic,retain) NSMutableArray *pointArray;
@end
//ChalkBoard.m
#import "ChalkBoard.h"
@implementation ChalkBoard
@synthesize pointArray;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
}
return self;
}
-(NSMutableArray *)pointArray
{
if(pointArray == nil) pointArray = [[NSMutableArray alloc]initWithCapacity:10];
return pointArray;
}
-(void)pushPoint:(CGPoint)point{
// this is called from drawtestViewController.m by button press
// in here the array is full and growing by each button press
[self.pointArray addObject:[NSValue valueWithCGPoint:point]];
NSLog(@"-pushPoint executed, size is %u, array: %@",[self.pointArray count], pointArray);
}
- (void)drawRect:(CGRect)rect{
// here array is always empty
NSLog(@"-DrawRect executed, size is %u, array: %@",[pointArray count], pointArray);
if ([pointArray count] >1){
... some irrelevant code
}
}
@end
NSLog output
2012-04-09 11:34:39.145 drawtest[5260:f803] -DrawRect executed, size is 0, array: (null)
2012-04-09 11:34:41.261 drawtest[5260:f803] -Button was pressed
2012-04-09 11:34:41.262 drawtest[5260:f803] -pushPoint executed, size is 1, array: (
"NSPoint: {100, 100}"
)
2012-04-09 11:34:41.264 drawtest[5260:f803] -pushPoint executed, size is 2, array: (
"NSPoint: {100, 100}",
"NSPoint: {10, 100}"
)
2012-04-09 11:34:41.266 drawtest[5260:f803] -DrawRect executed, size is 0, array: (null)
2012-04-09 11:34:42.825 drawtest[5260:f803] -Button was pressed
2012-04-09 11:34:42.826 drawtest[5260:f803] -pushPoint executed, size is 3, array: (
"NSPoint: {100, 100}",
"NSPoint: {10, 100}",
"NSPoint: {100, 100}"
)
2012-04-09 11:34:42.827 drawtest[5260:f803] -pushPoint executed, size is 4, array: (
"NSPoint: {100, 100}",
"NSPoint: {10, 100}",
"NSPoint: {100, 100}",
"NSPoint: {10, 100}"
)
2012-04-09 11:34:42.829 drawtest[5260:f803] -DrawRect executed, size is 0, array: (null)