Reputation: 145
I'm using a function in my DBChartViewController.m file to pull in a value from an SQLite database and pass it to another function in my draw2D.m file in order to draw a pie chart. The view displays initially, but never actually refreshes. Is there something wrong with this implementation?
Adding rest of files:
combinedDBandPieViewController.h
#import <UIKit/UIKit.h>
#import "/usr/include/sqlite3.h"
#import "draw2D.h"
@interface combinedDBandPieViewController : UIViewController {
draw2D *pieChart;
}
@property (nonatomic, retain) draw2D *pieChart;
- (IBAction) findContact;
@end
combinedDBandPieViewController.m
#import "combinedDBandPieViewController.h"
@implementation combinedDBandPieViewController
@synthesize pieChart;
-(void)findContact
{
...code to search DB...
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *pieField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
self.pie.text = pieField;
self.status.text = @"Match found";
self.pieChart.ratio = [pieField floatValue];
[self.pieChart setRatio:[pieField floatValue]];
}
}
draw2D.h
#import <UIKit/UIKit.h>
@interface draw2D : UIView {
@public float ratio;
}
@property (nonatomic,assign) float ratio;
-(void)setRatio:(float) newRatio;
@end
draw2D.m
#import "draw2D.h"
@implementation draw2D
@synthesize ratio;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect) rect{
NSLog(@"Hurray Reached Here"); //Only comes up once, at boot
int radius = 150;
NSLog(@"%f", ratio); //Only comes up once, displays 0.0000000
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 8.0);
CGRect rectangle = CGRectMake(84, 300, radius*2, radius*2);
CGContextAddEllipseInRect(context, rectangle);
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillPath(context);
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 234, 450);
CGContextAddArc(context, 234, 450, radius, 0, -(M_PI * (2*ratio)), 1);
CGContextFillPath(context);
}
-(void)setRatio:(float) newRatio
{
NSLog(@"Setting Ratio");
ratio = newRatio;
[self setNeedsDisplay];
}
- (void)dealloc
{
[super dealloc];
}
@end
Upvotes: 1
Views: 251
Reputation: 2471
From what I see above you are trying to create a view that uses a ratio to display a pie chart. In this case you would want to use the ratio to control what your view renders. I would modify the code to make the ratio a property of your view and whenever the ratio property is changed the view refreshes itself.
Expected behaviour
- (void) findContact
{
...code which pull value from database and displays correctly...
NSString *passingPie = pieField;
self.piechart.ratio = [passingPie floatValue];
}
.h file
@interface PieChartView : UIView{
@public
float ratio;
}
@property (nonatomic,assign) float ratio;
.m file
@implementation PieChartView
@synthesize ratio;
- (void) drawRect:(CGRect)rect{
int radius = 400;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 8.0);
CGRect rectangle = CGRectMake(184, 300, radius, radius);
CGContextAddEllipseInRect(context, rectangle);
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
CGContextFillPath(context);
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 384, 500);
CGContextAddArc(context, 384, 500, 200, 0, -(M_PI * (2*ratio)), 1);
CGContextFillPath(context);
}
- (void) setRatio:(float)newRatio{
ratio = newRatio;
[self setNeedsDisplay];
}
Upvotes: 2