查看不显示在iPhone应用程序中

时间:2012-07-24 11:57:18

标签: iphone objective-c view uiviewcontroller

我有4个文件,BarPlot.h和BarPlot.m,BarPlotViewController.h和BarPlotViewController.m 而我(可能非常直接向你们)是我不能让BarplotViewController显示我的图形。

我确保在故事板中调用了正确的视图控制器。

我已多次查看代码,但我无法弄清楚当我在模拟器中运行应用程序时为什么视图没有出现..

我已将这些文件包含在内。

非常感谢任何和所有帮助。

BarPlotViewController.h

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#import "BarPlot.h"

@interface BarPlotViewController : UIViewController {
    CPTGraphHostingView *_graphHostingView;
}

@property (nonatomic, retain) BarPlot *barPlot;

@end

BarPlotViewController.m

#import "BarPlotViewController.h"

@implementation BarPlotViewController
@synthesize barPlot = _barPlot;


- (void)viewWillAppear:(BOOL)animated
{   
    self.barPlot = [[BarPlot alloc] initWithHostingView:_graphHostingView];
    [self.barPlot generateBarPlot];
}

@end

BarPlot.h

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"

@interface BarPlot : UIViewController <CPTBarPlotDataSource, CPTBarPlotDelegate> {
    CPTGraphHostingView *_hostingView;  
CPTXYGraph *_graph;
}

@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, retain) CPTGraphHostingView *hostingView;
@property (nonatomic, retain) CPTXYGraph *graph;


// Methods to create this object and attach it to it's hosting view.
-(id)initWithHostingView:(CPTGraphHostingView *)hostingView;


// Generate the bar plot
- (void) generateBarPlot;

@end

BarPlot.m

#import "BarPlot.h"

@implementation BarPlot

#define BAR_POSITION @"POSITION"
#define BAR_HEIGHT @"HEIGHT"
#define COLOR @"COLOR"
#define CATEGORY @"CATEGORY"

#define AXIS_START 0
#define AXIS_END 50

@synthesize data;
@synthesize hostingView = _hostingView;
@synthesize graph = _graph;


// Initialise the bar plot in the provided hosting
-(id)initWithHostingView:(CPTGraphHostingView *)hostingView
{
    self = [super init];

if ( self != nil ) {
    self.hostingView = hostingView;
    self.graph = nil;
}


NSLog(@"Bar Plot: I'm in the initWithHostingView (in the barplot.m file)");

return self;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

    self.data = [NSMutableArray array];

    int bar_heights[] = {20,30};


    UIColor *colors[] = {
        [UIColor redColor],
        [UIColor blueColor],
        [UIColor orangeColor],
        [UIColor purpleColor]};


    NSString *categories[] = {@"", @""};



    for (int i = 0; i < 2 ; i++){
        double position = i*20; //Bars will be 10 pts away from each other
        double height = bar_heights[i];

        NSDictionary *bar = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithDouble:position],BAR_POSITION,
                             [NSNumber numberWithDouble:height],BAR_HEIGHT,
                             colors[i],COLOR,
                             categories[i],CATEGORY,
                             nil];
        [self.data addObject:bar];

    }
    [self generateBarPlot];
}
return self;
}


- (void)generateBarPlot
{
//Create host view
/*self.hostingView = [[CPTGraphHostingView alloc] 
                    initWithFrame:[[UIScreen mainScreen]bounds]];
[self.view addSubview:self.hostingView];



//Create graph and set it as host view's graph
self.graph = [[CPTXYGraph alloc] initWithFrame:self.hostingView.bounds];
[self.hostingView setHostedGraph:self.graph];*/



// Create a graph object which we will use to host just one scatter plot.
CGRect frame = [self.hostingView bounds];
self.graph = [[CPTXYGraph alloc] initWithFrame:frame];

// Tie the graph we've created with the hosting view.
self.hostingView.hostedGraph = self.graph;



//set graph padding and theme
self.graph.plotAreaFrame.paddingTop = 20.0f;
self.graph.plotAreaFrame.paddingRight = 20.0f;
self.graph.plotAreaFrame.paddingBottom = 70.0f;
self.graph.plotAreaFrame.paddingLeft = 70.0f;
[self.graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];



// Gets rid of decimal on years
NSNumberFormatter *labelFormatter = [[NSNumberFormatter alloc] init];
labelFormatter.maximumFractionDigits = 0;



//set axes ranges
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:
                    CPTDecimalFromFloat(AXIS_START)
                                                length:CPTDecimalFromFloat((AXIS_END - AXIS_START))];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:
                    CPTDecimalFromFloat(AXIS_START)
                                                length:CPTDecimalFromFloat((AXIS_END - AXIS_START))];




CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
//set axes' title, labels and their text styles
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @"Helvetica";
textStyle.fontSize = 14;
textStyle.color = [CPTColor whiteColor];



axisSet.xAxis.title = @"A";
axisSet.yAxis.title = @"B";
axisSet.xAxis.titleTextStyle = textStyle;
axisSet.yAxis.titleTextStyle = textStyle;
axisSet.xAxis.titleOffset = 30.0f;
axisSet.yAxis.titleOffset = 40.0f;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.yAxis.labelOffset = 3.0f;



//set axes' line styles and interval ticks
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor whiteColor];
lineStyle.lineWidth = 3.0f;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(5.0f);
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(5.0f);
axisSet.xAxis.majorTickLength = 10.0f;
axisSet.yAxis.majorTickLength = 10.0f;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.minorTicksPerInterval = 1;
axisSet.yAxis.minorTicksPerInterval = 1;
axisSet.xAxis.minorTickLength = .0f;
axisSet.yAxis.minorTickLength = .0f;
axisSet.xAxis.labelFormatter = labelFormatter;
axisSet.yAxis.labelFormatter = labelFormatter;



// Create bar plot and add it to the graph
CPTBarPlot *plot = [[CPTBarPlot alloc] init] ;
plot.delegate = self;
plot.dataSource = self;
plot.barWidth = [[NSDecimalNumber decimalNumberWithString:@"5.0"]
                 decimalValue];
plot.barOffset = [[NSDecimalNumber decimalNumberWithString:@"10.0"]
                  decimalValue];
plot.barCornerRadius = 5.0;



// Remove bar outlines
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor clearColor];
plot.lineStyle = borderLineStyle;



// Identifiers are handy if you want multiple plots in one graph
plot.identifier = @"chocoplot";
[self.graph addPlot:plot];
}



-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
if ( [plot.identifier isEqual:@"chocoplot"] ) 
{
    return [self.data count];
}

return 0;
}



-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
if ( [plot.identifier isEqual:@"chocoplot"] )
{

    NSDictionary *bar = [self.data objectAtIndex:index];


    if( fieldEnum == 2 ) {
        return [bar valueForKey:BAR_POSITION];
        //return [NSNumber numberWithDouble:20];
    }
    else if( fieldEnum == 3 ) {
        return [bar valueForKey:BAR_HEIGHT];
        //return [NSNumber numberWithDouble:20];
    }

}
NSLog(@"numberForPlot return before");
return [NSNumber numberWithDouble:15];
}

@end

1 个答案:

答案 0 :(得分:0)

嗯,我注意到的第一件事是你永远不会调用addSubview。这是实际上“添加视图”的代码,所以我从那里开始。另外,看到这个问题,第二个答案看起来可能会有所帮助:Adding CPTGraphHostingView as a subview