Adding CPTGraphHostingView as a subview

2019-08-02 06:51发布

问题:

There isn't a lot of questions about this and none of them were answered in a way that could solve my problem, still sorry if I'm asking something that was asked already.

My problem is that I'd like to use core plot to draw a graph inside a view. So I add the view on IB in my appviewcontroller.xib , but thenI can't find how to link that specific zone I just defined on IB to the file's owner.

A bit of precision: I managed to use core plot in another application, when it was the main view, but now that I want to add it to an existing application, so an existing view, it doesn't work...

And to add the new zone, I just went into IB, and dragged a view from the objects library onto my appviewcontroller.xib.

So how do I link that particular view to the code I wrote for it ?

Thanks for any help !

回答1:

Create another view(view 2) in a main view(default view let it be view)in the storyboard(xib file) and make sure it (view 2) as outlet in the header file(.h).

Drag and Connect it to the newly created view in the storyboard(xib file) so after making necessary connections in the storyboard,storyboard should be like this:

View---view (default view connected to view) and view 2---view (new view, view 2 connected to its view).

In the loadView method of implementation file,try out these code

-(void)loadView {
    [super loadView];

    newView = [[CPTGraphHostingView alloc] initWithFrame: [[UIScreen       mainScreen]applicationFrame]];

    [self.view2 addSubview:newView];

}


回答2:

U have to add uiview created to self.view,then add graph hosting view to it... Try this

- (void)loadView {
 self.view =view1;    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    CPGraphHostingView *hostingView = [[CPGraphHostingView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    hostingView.backgroundColor=[UIColor whiteColor];
    graph = [[CPXYGraph alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];   

    hostingView.hostedGraph = graph;
    [view1 addSubview:hostingView];