我在我的DBChartViewController.m文件中使用一个函数从SQLite数据库中提取一个值并将其传递给draw2D.m文件中的另一个函数以绘制饼图。视图最初显示,但从未实际刷新。这个实现有问题吗?
添加其余文件:
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
答案 0 :(得分:2)
从上面我看到的,您正在尝试创建一个使用比率来显示饼图的视图。在这种情况下,您可能希望使用比率来控制视图呈现的内容。我会修改代码以使比率成为视图的属性,并且每当比较属性更改时,视图都会自行刷新。
预期行为
- (void) findContact
{
...code which pull value from database and displays correctly...
NSString *passingPie = pieField;
self.piechart.ratio = [passingPie floatValue];
}
.h文件
@interface PieChartView : UIView{
@public
float ratio;
}
@property (nonatomic,assign) float ratio;
.m文件
@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];
}