2012-05-10 96 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