Search code examples
objective-cioscocoa-touchxcode4

Why is each line of code giving me "invalid context"?


This is my code in which every line is giving me an "invalid context" error when running the app in the iPad Simulator. Every statement after "self.Frame = frame; gives the error. How do I fix it?

- (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];
}

And this is the code which calls 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

Solution

  • You're manually calling drawRect, which I suspect is causing this problem. From the docs:

    You should never call this method directly yourself. To invalidate part of your view, and thus cause that portion to be redrawn, call the setNeedsDisplay or setNeedsDisplayInRect: method instead.

    This doesn't look necessary in this case, though. The view has been added as a subview of your view; it will automatically draw itself when your view renders.