2012-05-10 65 views
2

是iOS编程和核心图的新手。我正在使用XCode 4.2和iOS 5,并使用iOS Simulator测试iPhone应用程序。添加核心图饼图作为子视图 - 饼图没有绘制

我知道我在这里俯视一些东西(在这一天打破我的头一天以上&试图google'ing很多&尝试了各种可能的解决方案,都是徒劳)。有人可以请帮助或任何指针?

我试图做一件非常简单的事情 - >添加一个饼图(使用Core Plot完成)作为子视图。最初,我将饼图显示为模式视图&它工作正常。现在,在相同的视图中,我想添加几个按钮,因此我创建了一个视图(将在按下按钮时显示),其中我添加了2个按钮,并且还尝试添加此饼图。按钮显示正常,但饼图不可见!

这是所谓的“GraphView”的图,该获取当一个按钮的按下显示的XIB文件:

XIB screenshot showing the UI Components

在上述屏幕截图,突出显示的视图“查看”是在其中一个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马克 - 查看生命周期

- (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 

PIECHARTVI EW.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 

回答

1

你不需要gView财产。删除和更改pieChartView声明:

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

链接自定义视图这个属性,而不是gView和IB类变更为PieChartView。同时从-initWithNibName:bundle:中删除此属性的初始化 - 视图将在加载nib时自动初始化。

+0

太棒了!万分感谢,埃里克! :) 哇!这样可行!!谢谢:)(OMG ......经过两天的努力,感觉真棒!) – Jean