I am so close to the end but am really stuck, any help would be great.
So basically I am 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
This is the code in the ViewController responsible for generating the NSMutableArray and posting a notification that the array has changed
Code:
-(IBAction)calculate:(id)sender {
float xaxis=10
float d=20
NSString *const key = @"points";
[self willChangeValueForKey:key];
CGPoint pt = CGPointMake(xaxis, d);
[self.points addObject:[NSValue valueWithCGPoint:pt]];
[self didChangeValueForKey:key];
NSLog(@"fromviewpts%@", NSStringFromCGPoint(pt));
NSLog(@"fromviewnum%@", [NSString stringWithFormat:@"%d points", self.points.count ]);
}
and this is the code in the UIViewSubclass that is supposed to listen for changes to the array
Code:
- (void) ArrayChanged {
[points addObserver:self forKeyPath:@"points" options:0 context:NULL];
//[[points mutableArrayValueForKey:@"theArray"] addObject:[NSString string]];
}
- (void) observeValueForKeyPath: (NSString* ) keyPath ofObject: (id) object
change: (NSDictionary*) change context: (void*) context
{
NSAssert([keyPath isEqualToString:@"points"], @"Has Changed.");
NSLog(@"from assert%@", [NSString stringWithFormat:@"%d points", points.count ]);
}