這是我的代碼,在iPad模擬器中運行應用程序時,每行都會給我一個「無效上下文」錯誤。 。之後「self.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
非常感謝!就是這樣......呃!我知道這是一個重載......不知道爲什麼我沒有看到它...... – SpokaneDude 2012-04-01 19:16:11
我有一個相關的問題:「drawRect」是否被該類中的每個「繪圖」調用?換句話說,我是否需要爲每個不需要特定覆蓋的圖紙創建一個單獨的類? – SpokaneDude 2012-04-01 19:33:08
我不知道你是什麼意思的「繪圖」。每個'UIView'子類都會調用一次'drawRect'。默認的'drawRect'基本上只是繪製其背景,然後爲其所有子視圖調用'drawRect'。任何你需要做某些事情的視圖(如繪製'CGPath'或其他東西)需要實現'drawRect'。如果你沒有進行定製的核心圖形繪製,你根本不需要實現它,因爲UIKit已經內置了大多數渲染工具(例如,如果你正在繪製圖像,使用'UIImageView'等)。 – 2012-04-01 19:50:35