是iOS编程和Core Plot的新手。我正在使用XCode 4.2和iOS 5,并使用iOS模拟器测试iPhone应用程序。
我知道我在这里忽略了一些东西(在这一天突破了我的头脑并且经常尝试google'并尝试了各种可能的解决方案,都是徒劳的)。有人可以帮忙或指点一下吗?
我试图做一些非常简单的事情 - >添加饼图(使用Core Plot完成)作为子视图。最初,我将饼图显示为模态视图&它工作得很好。现在,在同一个视图中我想添加几个按钮,所以我创建了一个视图(按下按钮时会显示),我在其中添加了2个按钮,并尝试添加此饼图。按钮显示正常,但饼图不可见!
这是名为“GraphView”的视图的XIB文件,当按下按钮时会显示该文件:
在上面的屏幕截图中,突出显示的视图“View”是一个UIView对象,我想在其中显示饼图。整个视图'GraphView'显示正常;出现两个按钮,但我添加到子视图的饼图却没有。我在上图中作为子视图添加的UIView对象也可以正常显示(我通过为它设置背景颜色来检查它)。这是代码:
GRAPHVIEW.H
#import <UIKit/UIKit.h>
#import "PieChartView.h"
@interface GraphView : UIViewController
@property (strong, nonatomic) PieChartView *pieChartView;
// gView is the view which is linked to the 'UIView' subview in IB
// in the above figure
@property (strong, nonatomic) IBOutlet UIView *gView;
@property (strong, nonatomic) IBOutlet UIButton *buttonBack;
@property (strong, nonatomic) IBOutlet UIButton *buttonMail;
-(void) setHistoryData:(NSArray *)history;
...
...
@end
GRAPHVIEW.M
...
...
@synthesize gView;
@synthesize buttonBack;
@synthesize buttonMail;
...
...
-(void) setHistoryData:(NSArray *)history {
NSLog(@"GraphView: setHistoryData");
[pieChartView setHistoryData:history];
[gView addSubview:pieChartView];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
pieChartView = [[PieChartView alloc] init];
return self;
}
...
...
#pragma mark - 查看生命周期
- (void)viewDidLoad
{
NSLog(@"GraphView: viewDidLoad");
[super viewDidLoad];
[pieChartView initializeGraph];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
PIECHARTVIEW.H 这是绘制饼图的那个(它应该'画')饼图!
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
// I tied subclassing 'UIViewController' or just 'UIView'...all in vain
@interface PieChartView : CPTGraphHostingView <CPTPlotDataSource> {
CPTXYGraph *graph;
CPTPieChart *pieChart;
}
@property (nonatomic, retain) CPTXYGraph *graph;
-(void) initializeGraph;
-(void) initializePieChart;
-(void) setHistoryData:(NSArray *)history;
@end
PIECHARTVIEW.m
...
@implementation PieChartView
@synthesize graph;
NSArray *historyData;
// I added the NSLog to see if these get called, but they don't seem to get called!
// So, this means that the pie chart is not being drawn actually!
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
NSLog(@"numberOfRecordsForPlot: History count: %d", (int)[historyData count]);
return [historyData count];
}
// This too isn't getting called
-(NSNumber *) numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
NSLog(@"historyView: numberForPlot");
return [historyData objectAtIndex:index];
}
// This too is not getting called! OMG!
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index {
NSLog(@"HistoryView: dataLabelForPlot");
...
}
// This method is getting called. Am seeing the log messages
-(void) initializeGraph {
NSLog(@"HistoryView: initializeGraph");
graph = [[CPTXYGraph alloc] init];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[graph applyTheme:theme];
CPTGraphHostingView *hostingView = (CPTGraphHostingView *) self;
hostingView.hostedGraph = graph;
//hostingView.bounds = CGRectMake(5, 5, 70, 70);
[self initializePieChart];
}
// This method is also getting called. I see the log messages from this method too
/**
* Initialize the pie chart for display
*/
-(void) initializePieChart {
NSLog(@"HistoryView: initializePieChart");
pieChart = [[CPTPieChart alloc] init];
pieChart.dataSource = self;
pieChart.pieRadius = 100.0;
pieChart.opaque = FALSE;
//pieChart.pieRadius = 60;
pieChart.shadowOffset = CGSizeMake(-1, 1);
pieChart.identifier = @"PieChart";
pieChart.startAngle = M_PI_4;
pieChart.sliceDirection = CPTPieDirectionCounterClockwise;
pieChart.labelOffset = -0.6;
[graph addPlot:pieChart];
NSLog(@"added pie chart to the graph view");
}
// This also is getting called
-(void) setHistoryData:(NSArray *)history {
NSLog(@"HistoryView: setHistoryData");
historyData = history;
}
...
...
@end
答案 0 :(得分:1)
您不需要gView
属性。删除它并将pieChartView
声明更改为:
@property (strong, nonatomic) IBOutlet PieChartView *pieChartView;
将自定义视图链接到此属性而不是gView
,并将IB中的类更改为PieChartView
。同时从-initWithNibName:bundle:
中删除此属性的初始化 - 当加载nib时,视图将自动初始化。