2011-08-31 45 views
0

林与出Interface Builder的一个项目工作的基本问题,约CoreGraphics在

我想画一条线,测试方,但不工作 这里的代码

- (void)drawRect:(CGRect)rect { 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextClearRect(context, rect); 
//Set the stroke (pen) color 
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
//Set the width of the pen mark 
CGContextSetLineWidth(context, 5.0); 
    //Start at this point 
//CGContextMoveToPoint(context, 10.0, 30.0); 
CGContextMoveToPoint(context, 20, 20); 
    //Give instructions to the CGContext 
//(move "pen" around the screen) 
CGContextAddLineToPoint(context, 310.0, 30.0); 
CGContextStrokePath(context); 
// Draw a red solid square 
CGContextSetRGBFillColor(context, 255, 0, 0, 1); 
CGContextFillRect(context, CGRectMake(10, 10, 50, 50)); 
} 
- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 
self.view.backgroundColor = [UIColor grayColor]; 
    UILabel *labelA = [[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 20)]autorelease]; 
     labelA.text = @"mk ss9"; 
[self.view addSubview:labelA]; 
} 

标签显示正常,但线条和方形不显示,缺少什么?

非常感谢!

回答

0

好了!

的问题是,我试图在我的ViewController绘制,这是不是这样,

所以我创建了一个新的类,它有子类的UIView,现在工作得很好!