UIView没有正确加载?

时间:2013-03-14 17:17:31

标签: iphone ios objective-c cocos2d-iphone

我的UIView代码文件在storyboard中设置了视图的自定义类。但是,当我运行应用程序并加载视图时,没有任何反应?我没有获得任何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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

尝试添加此代码:

- (void)setup{
    self.contentMode=UIViewContentModeRedraw;
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
- (void)awakeFromNib{
    [self setup];
}