0

我的 UIView 的代码文件在情节提要中设置了视图的自定义类。但是,当我运行应用程序并加载视图时,什么都没有发生?我没有得到任何 NSLog,屏幕上没有任何内容?有谁知道我做错了什么?我没有正确地处理某些事情吗?

我的 UIView 代码:

- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"Drawling area loaded");
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{

    CGPoint dotOne = CGPointMake(1, 1);
    [squareLocations addObject:[NSValue valueWithCGPoint:dotOne]];

    CGPoint dotTwo = CGPointMake(10, 10);
    [squareLocations addObject:[NSValue valueWithCGPoint:dotTwo]];

    CGPoint dotThree = CGPointMake(100, 100);
    [squareLocations addObject:[NSValue valueWithCGPoint:dotThree]];


    int numby = [squareLocations count];

    for (int i = 0; i < numby; i++)
    {
        NSValue *pointLocation = [squareLocations objectAtIndex:i];
        CGPoint tmpPoint = [pointLocation CGPointValue];

        CGRect tmpRect = CGRectMake(tmpPoint.x, tmpPoint.y, 10, 10);

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, 5.0);
        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
        CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
        CGColorRef color = CGColorCreate(colorspace, components);
        CGContextSetStrokeColorWithColor(context, color);
        CGContextMoveToPoint(context, tmpPoint.x, tmpPoint.y);
        CGContextAddLineToPoint(context, 300, 400);
        CGContextStrokePath(context);

    }
}


@end
4

2 回答 2

1

我认为你应该使用调试器来确保你的ivar在方法squareLocations中不是nildrawRect:。您可以向nil发送任何消息而不会出现任何错误,并且在 Objective-c 中实际上什么也不做。在方法中初始化squareLocationsivardrawRect:也可以解决问题。

于 2013-03-14T17:36:50.527 回答
0

尝试添加此代码:

- (void)setup{
    self.contentMode=UIViewContentModeRedraw;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
- (void)awakeFromNib{
    [self setup];
}
于 2013-03-16T04:27:11.947 回答