ios – 这个错误是无效的上下文0x0?

我在ViewDidLoad中编写了以下代码

// Implement viewDidLoad to do additional setup after loading the view,typically     from      a nib.
     -(void)viewDidLoad {
    [super viewDidLoad];

Nsstring *str = [Nsstring stringWithFormat:@"Veer"];

CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGFloat values[4] = {55.0,0.0,1.0}; 
CGColorRef glowColor = CGColorCreate(rgbColorspace,values); 
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShadowWithColor( context,CGSizeMake( 5.0,0.0 ),20.0f,glowColor);   
[str drawAtPoint:CGPointMake(10.5f,0.5f) withFont:[UIFont      fontWithName:@"Helvetica" size:100.0f]];    
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60,200,250,100)];
[label setBackgroundColor:[UIColor clearColor]];
label.font=[UIFont fontWithName:@"DBLCDTempBlack" size:50.0];
label.textColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
[label setText:str];

[self.view addSubview:label];
  }

在Console中我看到了

Testing Clock[3286] <Error>: CGContextSetStyle: invalid context 0x0
 Testing Clock[3286] <Error>: CGContextSetFont: invalid context 0x0
 Testing Clock[3286] <Error>: CGContextSetTextMatrix: invalid context 0x0
 Testing Clock[3286] <Error>: CGContextSetFontSize: invalid context 0x0
 Testing Clock[3286] <Error>: CGContextSetTextPosition: invalid context 0x0
 Testing Clock[3286] <Error>: CGContextShowGlyphsWithAdvances: invalid context 0x0

解决方法

您应该将该自定义绘图代码移出viewDidLoad并将其移动到drawRect.

如果要实现自己的drawRect方法,还需要对视图进行子类化.

查看此示例:http://ykyuen.wordpress.com/2010/08/27/iphone-uiview-with-border/

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...