My code is working so far but I had to create a Class for the UIView. This is a bit inconvenient because I need to interact with the ViewController too.
BTW, I did try [self setNeedsDisplay] on the ViewDidLoad of the UIViewController subclass file but it didn’t work.
Here’s the code, which works on UIView Subclass but doesn’t get called on a UIViewController one:
- (void)drawRect:(CGRect)rect
{
UIColor *currentColor = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
someNum = 1;
CGContextBeginPath(context);
CGContextMoveToPoint(context, 30, 40);
[self addDotImageX:30 andY:40];
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextStrokePath(context);
}
Any ideas on what to try? BTW, this is a TabBar App. I know those can somehow block the calls to drawRect.
The Tabs where created programatically, not through a Nib. Eg:
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
UIViewController *vc;
vc = [[Education alloc] init];
vc.title = @"Education";
[listOfViewControllers addObject:vc];
vc.tabBarItem.image = [UIImage imageNamed:@"info.png"];
[vc release];
I would appreciate any ideas. I’ve been through the answers on this site related to setNeedsDisplay not calling drawRect and haven’t found an answer for my particular case.
Thanks.
You are mixing up two classes. A UIViewController is not a UIView, meaning it doesn’t inherits from UIView. But the good news is it has a view, declared as property: It’s composition. The drawRect method is only available in a UIView class/subclass.
If you like to force the controller’s view to redraw you can call
in the viewController.
You can set your own custom view as the view of your viewController with the loadView method. It could look like this:
So you can keep your drawing code separated in your MySubclassOFUIView.m file.
About the UIViewController:
And the purpose of a UIView:
Have a look at Cocoa Core Competencies / Model-View-Controller in Apple’s official documentation, it describes the MVC design pattern.