gpt4 book ai didi

iphone - 添加 Core Plot 饼图作为 subview - 饼图未绘制

转载 作者:行者123 更新时间:2023-12-03 21:17:14 25 4
gpt4 key购买 nike

我是 iOS 编程和 Core Plot 的新手。我使用 XCode 4.2 和 iOS 5,并使用 iOS 模拟器测试 iPhone 应用程序。

我知道我忽略了这里的一些东西(为此我花了一天多的时间并尝试了很多谷歌搜索并尝试了各种可能的解决方案,但都是徒劳)。有人可以帮忙或指点一下吗?

我正在尝试做一些非常简单的事情 -> 添加饼图(使用 Core Plot 完成)作为 subview 。最初,我将饼图显示为模态视图,效果很好。现在,在同一个 View 中,我想添加几个按钮,因此我创建了一个 View (按下按钮时将显示该 View ),在其中添加了 2 个按钮,并尝试添加此饼图。按钮显示正常,但看不到饼图!

这是名为“GraphView”的 View 的 XIB 文件,按下按钮时会显示该 View :

XIB screenshot showing the UI Components

在上面的屏幕截图中,突出显示的 View “View”是一个 UIView 对象,我想在其中显示饼图。整个 View “GraphView”显示良好;这两个按钮出现,但我添加到 subview 的饼图没有出现。我在上图中添加为 subview 的 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

PIECARTVIEW.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

PIECARTVIEW.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

最佳答案

您不需要 gView 属性。删除它并将 pieChartView 声明更改为:

@property (strong, nonatomic) IBOutlet PieChartView *pieChartView;

将自定义 View 链接到此属性而不是 gView,并将 IB 中的类更改为 PieChartView。还要从 -initWithNibName:bundle: 中删除此属性的初始化 - 加载 Nib 时 View 将自动初始化。

关于iphone - 添加 Core Plot 饼图作为 subview - 饼图未绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10536327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com