Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple UIView drawRect not being called

Tags:

ios

uiview

I can't figure out what the problem is here. I have a very simple UIViewController with a very simple viewDidLoad method:

-(void)viewDidLoad {

    NSLog(@"making game view");
    GameView *v = [[GameView alloc] initWithFrame:CGRectMake(0,0,320,460)];

    [self.view addSubview:v];

    [super viewDidLoad];
}

And my GameView is initialized as follows:

@interface GameView : UIView {

and it simply has a new drawRect method:

- (void)drawRect:(CGRect)rect
{

    [super drawRect:rect];

    NSLog(@"drawing");
}

In my console, I see "making game view" being printed, but "drawing" never is printed. Why? Why isn't my drawRect method in my custom UIView being called. I'm literally just trying to draw a circle on the screen.

like image 606
CodeGuy Avatar asked Jun 07 '11 21:06

CodeGuy


1 Answers

Have you tried specifying the frame in the initialization of the view? Because you are creating a custom UIView, you need to specify the frame for the view before the drawing method is called.

Try changing your viewDidLoad to the following:

NSLog(@"making game view");
GameView *v = [[GameView alloc] initWithFrame:CGRectMake(0,0,320,460)];


if (v == nil)
    NSLog(@"was not allocated and/or initialized");

[self.view addSubview:v];

if (v.superview == nil)
    NSLog(@"was not added to view");

[super viewDidLoad];

let me know what you get.

like image 121
Eytan Avatar answered Oct 14 '22 17:10

Eytan