Line Graph CGPoints from NSMutableArray

Posted by Mattog1456 on Stack Overflow See other posts from Stack Overflow or by Mattog1456
Published on 2011-01-11T05:44:01Z Indexed on 2011/01/12 13:53 UTC
Read the original article Hit count: 144

Filed under:
|
|

I have been trying to adapt the code from the accelerometer example such that when the user depresses a uibutton a point is added to a line graph.

Working on the converting two floats, which are the result of calculate as below into a CGPoint and converting the CGPoint into an NSValue and then adding this to a NSMutableArray with the following

-(IBAction)calculate:(id)sender {   
    self.points = [NSMutableArray array];
    CGPoint pt = CGPointMake(d, c);
    [self.points addObject:[NSValue valueWithCGPoint:pt]];
    NSLog(@"%@", NSStringFromCGPoint(pt));
    NSLog(@"%@", [NSString stringWithFormat:@"%d points", self.points.count ]);
}   

But for some reason I am only getting one object stored in the array, it seems everytime push the calculate button the object pt gets overwritten, on the plus side it has the correct x,y coords. Any ideas on this one?

UPDATE

Removed self.points = [NSMutableArray array]; and placed it in view did load, also set the first points to 0,0. so that is working ok. Now the next problem is that I have a Graph subclass where the CG Drawing is taking place. I am trying to figure out a simple way to be able to access the above NSMutableArray which is in a ViewController class from the graph class.

Am so close to the end but am really stuck, any help would be great. Still trying to draw a line graph on a UIView which is on a UIScrollview. The draw rect method is in the UIView Subclass and everything is working there, I have gridlines and labels on the axis and I can draw manually onto it. But the problem I have is that I cannot read the NSMutableArray of the CGPoints, which are the x and y coords. The ViewController performs a calculation and the results are written to the NSMutable array and this is all working fine as well, I can see the CGpoints and their values being written with NSLogs in the ViewController. I have tried various ways to set the NSMutableArray up as a global but to no avail, everything runs but while I can see the points being written in the ViewController they are just not visible to the UIView Subclass. I have also tried to use the addObserver and observeValueForKeyPath methods and once again while everything runs the subclass cannot see the array. Any ideas, suggestions, tips or thoughts would be great

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c