这是我的代码,当在iPad模拟器中运行应用程序时,其中的每一行都会给我一个“无效上下文”错误。"self.Frame = frame;“之后的每一条语句都会给出错误。我该如何修复它?
- (void)makeFrame:(CGRect)frame number:(NSInteger) number color:(UIColor *) color {
float rd1 = 225.00/255.00;
float gr1 = 102.00/255.00;
float bl1 = 0.00/255.00;
float rd2 = 225.00/255.00;
float gr2 = 153.00/255.00;
float bl2 = 0.00/255.00;
self.frame = frame;
self.backgroundColor = [UIColor colorWithRed:rd1 green:gr1 blue:bl1 alpha:1.0];
[[self layer] setBorderColor:[[UIColor blackColor] CGColor]];
[[self layer] setBorderWidth:0.5];
[[self layer] setCornerRadius:10];
self.tag = number; // set each button's tag
[self setClipsToBounds: YES];
// do date label
date = [[UILabel alloc]initWithFrame:CGRectMake(10, 2, 65, 40 )];
date.backgroundColor = [UIColor clearColor]; // x y w h
date.textColor = [UIColor colorWithRed:rd2 green:gr2 blue:bl2 alpha:1.0];
date.text = [NSString stringWithFormat:@"%d", number];
date.font = [UIFont boldSystemFontOfSize:40];
// date.alpha = 0.2;
[self addSubview:date];
}这是调用makeFrame的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
allButtons = [NSMutableArray array];
for(int k = 40, m = 0; m < 6; m++, k+= 42) // this controls the vertical distance between rows
for(int i = 0, j=32; i < 7; i++, j+=102) { // this controls the size and horizontal distance
calendarButton *cb= [calendarButton buttonWithType:UIButtonTypeCustom];
[cb makeFrame:CGRectMake(j, k, 100, 40) number: allButtons.count+1 color:[UIColor orangeColor]];
// x y w h
[self.view addSubview:cb];
[allButtons addObject:cb]; // put it in the array
}
calendarButton *cb= [calendarButton buttonWithType:UIButtonTypeCustom];
[cb drawRect:CGRectMake(0,0, 100, 40)]; // 150发布于 2012-04-02 03:12:48
您正在手动调用drawRect,我怀疑这是导致此问题的原因。从文档中:
您永远不应该自己直接调用此方法。要使视图的一部分无效,从而导致该部分被重新绘制,请改为调用
setNeedsDisplay或setNeedsDisplayInRect:方法。
不过,在这种情况下,这看起来不是必要的。该视图已被添加为视图的子视图;当视图呈现时,它将自动绘制自身。
发布于 2012-04-02 03:09:16
您正在尝试访问视图控制器中的视图属性。在您的情况下,self为UIViewController (或sublcass),请将其更改为self.view以访问视图
例如。
self.view.frame = frame;https://stackoverflow.com/questions/9967252
复制相似问题