2012-05-29 21 views
0

我試圖從Class A調用繪圖方法,例如位於Class B中的方法,該方法正在調用,但沒有繪圖發生。iPhone Quartz - 從其他類別調用繪圖方法

- (void)drawIt 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); 

    NSString *string = [NSString stringWithString:@"TEXT"]; 
    [string drawAtPoint:CGPointMake(12, 51) withFont:[UIFont fontWithName:@"Helvetica" size:35.0f]]; 
} 

爲什麼我可以從其他類調用此方法?

回答

0

如果您使用的是UIView或某個子類,則需要重載drawRect方法。所以,在drawRect裏面,你可以在其他類中調用你的方法。另外,你也可以通過參數傳遞你的上下文。

+0

謝謝,調用超類是UITableView,我正在使用drawRect,但我不想在對象init上畫圖。 – jkigel

1

首先創建UIView的子類「YourView」。寫入分配代碼viewDidLoad方法是在B級

- (void)viewDidLoad{ 
YourView *temp = [[YourView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    [self.view addSubview:temp]; 
} 

在YourView.m

- (void)drawRect:(CGRect)rect 
{ 
CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); 
    NSString *string = [NSString stringWithString:@"TEXT"]; 
    [string drawAtPoint:CGPointMake(12, 51) withFont:[UIFont fontWithName:@"Helvetica" size:35.0f]]; 
} 

實現- (void)drawRect:(CGRect)rect方法我認爲這將是對你有所幫助。

+0

謝謝,但我不想在初始化後直接繪製此圖... 我也希望能夠手動繪製和擦除圖形 – jkigel

相關問題