mbogh
mbogh

Reputation: 1361

ViewController and drawRect

I have a UIViewController and I would like it to call drawRect so i can draw on the view but nothing happens.

#import <UIKit/UIKit.h>

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

@end

and the implementation

#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

But no pony gets drawn on the view when the app is run, what do i do wrong?

Upvotes: 0

Views: 5526

Answers (2)

Victor
Victor

Reputation: 3527

The UIViewController don't have overridden message drawRect. You should create custom class derived from UIView and override message drawRect there.

You can override message (UIView *)view for UIViewController and return own custom UIView or in Interface Builder change class from UIView to own class.

Upvotes: 6

Jeanne
Jeanne

Reputation: 85

Calling setNeedsDisplay on your UIView, not the ViewController, is one way of triggering and re-triggering your drawRect code, and you can send this message conditionally whenever you need to update the entire view.

You can call it within a custom UIView using

[self.setNeedsDisplay]

or from the View Controller by making the UIView a property of the controller and calling

[self.myView setNeedsDisplay]

Upvotes: 0

Related Questions