2010-06-24 52 views
0

我有一個UIViewController,我希望它調用drawRect,所以我可以在視圖上繪製,但沒有任何反應。ViewController和drawRect

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController { 
...code... 
} 

@end 

和實現

#import "ViewController.h" 


@implementation ViewController 

-(void) drawRect:(CGRect) rect { 
draw a pony 
} 

- (void)viewDidLoad { 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 

但沒有得到小馬上畫時,應用程序運行的觀點,我該怎麼辦錯了嗎?

回答

4

UIViewController沒有覆蓋消息drawRect。 您應該創建從UIView派生的自定義類,並覆蓋此處的消息drawRect

您可以重寫消息(UIView *)viewUIViewControllerUIView返回自己的自定義UIView或在Interface Builder改變類自己的類。

0

調用setNeedsDisplayUIView,而不是ViewController,是觸發和重新觸發您的drawRect代碼的一種方式,而當你需要更新整個視圖中,可以有條件地發送該消息。

您可以使用自定義UIView中調用它

[self.setNeedsDisplay] 

或從視圖控制器通過使UIView控制器的屬性和調用

[self.myView setNeedsDisplay]