The context is that I am trying to plot a series of temperatures as a graph.
At the moment my application works as follows (as i understand it):
The application opens, openfile is used which initialises my document class this reads the modified csv file into an NSString “fileContents” which I then separate into an array of strings, each string containing the value without separaters or whitespace. As follows:
NSArray *temps = [fileContents componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"\n|\r"]];
After that I initialise my custom view, the TemperatureGraph class, and immediately pass it the array which becomes the currentlog seen in the code below. Instance id is current.
Finally I invoke:
[current drawpath];
Which is the instance method below:
- (void) drawpath
{
NSBezierPath *program = [NSBezierPath bezierPath];
NSPoint st = NSMakePoint(0, 0);
[program moveToPoint:st];
[program setLineWidth:10];
[program setLineCapStyle:NSRoundLineCapStyle];
[[NSColor blueColor] setStroke];
[program stroke];
NSUInteger a, b;
a = [currentlog count];
b = 0;
while (b <= a) {
NSPoint new;
float x;
x = 5 * b;
new = NSMakePoint(x, [[currentlog objectAtIndex:b] floatValue]);
[program lineToPoint:new];
[program stroke];
b += 6;
}
}
Why won’t it work? Definition of won’t work: Application compiles and runs but I have a blank window.
Also please point out any rookie errors that are in there but don’t affect code, as I am still in the learning stages here.
Drawing doesn’t happen unless you have an active graphics context. I’m assuming the
drawpathmethod above is inside an NSView subclass – well in there you need to override a method calleddrawRect:– it may even have been added in as a stub when you made the subclass?Cocoa calls this method when it is time for the view to draw itself. This means that, when it is being called, a graphics context will be set up and and drawing code you do will actually appear on the screen.
Simply call your
drawpathmethod from insidedrawRect:and you should see your lines.The general point to remember is that you don’t call any drawing code yourself (except from within
drawRect:– you either mark a view as needing display ([myView setNeedsDisplay:YES];, or let the system do that itself.