在iOS中使用添加的变量刷新UIView函数



我正在使用DBChartViewController.m文件中的一个函数从SQLite数据库中提取一个值,并将其传递给draw2D.m文件的另一个函数,以便绘制饼图。视图最初显示,但从未实际刷新。这个实现有什么问题吗?

添加其余文件:

组合DB和PieViewController.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

组合数据库和PieViewController.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

从我上面看到的内容来看,您正试图创建一个使用比率来显示饼图的视图。在这种情况下,您可能希望使用该比例来控制视图渲染的内容。我会修改代码,使ratio成为视图的一个属性,并且每当ratio属性更改时,视图都会自行刷新。

预期行为

- (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];
}

最新更新