• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

objective-c - drawRect 查看空数组

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:27:40 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

drawtestViewController.m 中的按钮链接到 ChalkBoard.m 中的 pushPoint Action 。它用 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)pushPointCGPoint)point;
@property (nonatomic,retain) NSMutableArray *pointArray;
@end

//ChalkBoard.m
#import "ChalkBoard.h"

@implementation ChalkBoard
@synthesize pointArray;

- (id)initWithFrameCGRect)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)pushPointCGPoint)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)drawRectCGRect)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)



Best Answer-推荐答案


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

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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap