2013-03-14 151 views
0

我有我的UIView的代碼文件在故事板中設置視圖的自定義類。但是,當我運行應用程序並加載視圖時,什麼都沒有發生?我沒有得到任何我的NSLogs,沒有任何東西被吸引到屏幕上?有誰知道我做錯了什麼?我沒有正確地使用某些東西嗎?UIView無法正確加載?

我的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 

回答

1

我認爲你應該使用調試器,以確保您的squareLocations伊娃不在drawRect:方法。您可以發送任何消息到沒有任何錯誤,實際上在objective-c中什麼都不做。也可以通過drawRect:方法初始化squareLocations伊娃來解決問題。

0

嘗試添加以下代碼:

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