Iphone UIView无法正确加载?
我将UIView的代码文件设置为脚本中视图的自定义类。但是,当我运行应用程序并加载视图时,什么都没有发生?我没有得到任何NSLog,没有任何东西被吸引到屏幕上?有人知道我做错了什么吗?我是不是对某些事情做了适当的改进 UIView的我的代码:Iphone UIView无法正确加载?,iphone,ios,objective-c,cocos2d-iphone,Iphone,Ios,Objective C,Cocos2d Iphone,我将UIView的代码文件设置为脚本中视图的自定义类。但是,当我运行应用程序并加载视图时,什么都没有发生?我没有得到任何NSLog,没有任何东西被吸引到屏幕上?有人知道我做错了什么吗?我是不是对某些事情做了适当的改进 UIView的我的代码: - (id)initWithFrame:(CGRect)frame { NSLog(@"Drawling area loaded"); self = [super initWithFrame:frame]; if (self) {
- (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
-(id)initWithFrame:(CGRect)frame
{
NSLog(@“加载的牵引区”);
self=[super initWithFrame:frame];
如果(自我){
//初始化代码
}
回归自我;
}
-(void)drawRect:(CGRect)rect
{
CGPoint dotOne=CGPointMake(1,1);
[squareLocations添加对象:[NSValue valueWithCGPoint:dotOne]];
cgpointdotwo=CGPointMake(10,10);
[squareLocations添加对象:[NSValue valueWithCGPoint:dotTwo]];
CGPoint dotThree=CGPointMake(100100);
[squareLocations添加对象:[NSValue valueWithCGPoint:dotThree]];
int numby=[平方位置计数];
for(int i=0;i
我认为您应该使用调试器来确保drawRect:
方法中的squareLocations
ivar不是nil。在objective-c中,您可以向nil发送任何消息,而不会出现任何错误,也不会执行任何操作。另外,在drawRect:
方法中初始化squareLocations
ivar可以解决此问题。尝试添加以下代码:
- (void)setup{
self.contentMode=UIViewContentModeRedraw;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)awakeFromNib{
[self setup];
}