objective-c - 绘制矩形看到空数组

标签 objective-c ios nsmutablearray drawrect

drawtestViewController.m 中的按钮链接到 ChalkBoard.m 中的 PushPoint 操作。它用 CGpoint 对象填充 pointArray。这个数组到处都充满了对象,除了从drawRect查看时除外。似乎从drawRect我以某种方式引用原始数组的空副本,但我找不到任何错误......

我已将整个项目上传到 http://dl.dropbox.com/u/42099382/drawtest.zip


drawtestViewController.m 中的按钮操作

- (IBAction)myDo {
   NSLog(@"-Button was pressed");    
  [self.board pushPoint:CGPointMake(100, 100 )];
  [self.board pushPoint:CGPointMake(10, 100 )];
  [self.graphicView setNeedsDisplay];
}

//  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)

最佳答案

问题实际上不在您的 Chalkboard 类中,而是在您的 drawtestViewcontroller 中。您的 myDo 操作将点添加到 self.board,但将 setNeedsDisplay 消息发送到 self.graphicView,即也是一个黑板,但与您添加点的黑板不同。

因此您看到的第二条日志消息实际上来自一个完全不同的对象。

关于objective-c - 绘制矩形看到空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10070872/

相关文章:

ios - 告诉 iOS 不要调整 SKNode 动画纹理的大小?

iphone - UIPickerView 出现但不显示数据

IOS- View 在横向应用程序中定义但以纵向模式运行

ios - 如何通过按钮自动创建标签?

iphone - 如何检查 iOS 设备是否为视网膜显示屏 iPod touch?

arrays - 如何将元组的快速数组转换为 NSMutableArray?

ios - NSMutableArray 为图像创建问题

objective-c - 将 UIImage 设置为背景 - 不平铺

objective-c - 截断 NSTextView?

ios - 在 iOS 中使用 AFNetworking 下载文件