2

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

我知道我在这里忽略了一些东西(在这件事上打破了一天多的时间并尝试了很多谷歌并尝试了各种可能的解决方案,但都是徒劳的)。有人可以帮忙或任何指示吗?

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

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

显示 UI 组件的 XIB 屏幕截图

在上面的屏幕截图中,突出显示的视图“视图”是一个 UIView 对象,我想在其中显示饼图。整个视图“GraphView”显示良好;两个按钮出现,但我添加到子视图的饼图没有。我在上图中添加为子视图的 UIView 对象也可以正常显示(我通过为其设置背景颜色来检查它)。这是代码:

图形视图.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

图形视图.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

PICHARTVIEW.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
4

1 回答 1

1

你不需要gView财产。删除它并将pieChartView声明更改为:

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

将自定义视图链接到此属性,而不是将gViewIB 中的类更改为PieChartView. 还要删除此属性的初始化-initWithNibName:bundle:——加载 nib 时视图将自动初始化。

于 2012-05-11T02:15:56.390 回答