Iphone OBJ-C:从单独的视图控制器调用core plot graph的实例
我使用的是视图控制器,即ViewController:UIViewController,还有另一个类GraphViewController:UIViewController 如何调用GraphViewController实例并将其放入我的ViewController中?我目前正试图直接调用ViewController中的绘图,但我希望使图形模块化,这样,如果再次使用相同的图形,就不必复制代码 ViewController具有多个方法Iphone OBJ-C:从单独的视图控制器调用core plot graph的实例,iphone,objective-c,xcode,macos,core-plot,Iphone,Objective C,Xcode,Macos,Core Plot,我使用的是视图控制器,即ViewController:UIViewController,还有另一个类GraphViewController:UIViewController 如何调用GraphViewController实例并将其放入我的ViewController中?我目前正试图直接调用ViewController中的绘图,但我希望使图形模块化,这样,如果再次使用相同的图形,就不必复制代码 ViewController具有多个方法 -(void) refreshGraph; //Inheret
-(void) refreshGraph;
//Inhereted Methods
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot;
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index;
如何将这些方法移动到单独的类中,然后调用它以在ViewController中获得相同的绘图?
我肯定这上面一定有线索,但我好像找不到
编辑:
这是对我问题的澄清。当我采用CPPlotDataSource协议并在一个类中完成所有绘图设置时,我的代码可以工作。我想做的是将所有的图形设置移到另一个类中,以便有效地将所有核心的绘图图形和设置函数从我的主ViewController类中分离出来
以下是我的主ViewController RatesTickerViewController.h和GraphViewController RatesTickerGraphController.h的一些相关代码
#import "RatesTickerViewController.h"
#import "Tick.h"
#import <Foundation/Foundation.h>
#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)
@implementation RatesTickerViewController
@synthesize graphController, graphView;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
RatesTickerGraphController *aGraphController = [[RatesTickerGraphController alloc] initWithNibName:@"RatesTickerViewController" bundle:[NSBundle mainBundle]];
[self setGraphController:aGraphController];
[aGraphController release];
//self.graphView = self.graphController.layerHost;
[self.graphController refreshGraph];
}
#pragma mark -
#pragma mark Plot Data Source Methods
-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot {
return [graphController.dataForPlot count];
}
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSNumber *num = [[graphController.dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
return num;
}
#pragma mark -
#pragma mark Set Rotation Orientation Methods
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if (fromInterfaceOrientation == UIInterfaceOrientationPortrait)
{
isShowingLandscapeView = YES;
[self setViewToLandscape];
} else {
isShowingLandscapeView = NO;
[self setViewToPortrait];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[graphController release]; graphController = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
如果我理解正确,您希望有一个通用视图控制器在其视图中设置核心打印图,但希望该视图能够位于其他视图中。要做到这一点,只需在视图控制器中为较大视图创建一个graph view controller实例,并将graph view controller的视图添加为较大视图控制器视图的子视图 就您描述的passthrough委托方法而言,您可以创建一个需要实现这两个方法的GraphViewController delegate协议,并在GraphViewController上创建一个委托属性,如下所示:
id<GraphViewControllerDelegate> delegate;
id代表;
在GraphViewController对核心Plot委托方法的实现中,只需让它们调用委托对这些方法的实现即可。将包含视图控制器设置为GraphViewController的代理,并通过实现这两种方法使其符合GraphViewController delegate协议。如果我理解正确,您希望有一个通用视图控制器,在其视图中设置核心绘图图,但您希望该视图能够位于其他视图中。要做到这一点,只需在视图控制器中为较大视图创建一个graph view controller实例,并将graph view controller的视图添加为较大视图控制器视图的子视图 就您描述的passthrough委托方法而言,您可以创建一个需要实现这两个方法的GraphViewController delegate协议,并在GraphViewController上创建一个委托属性,如下所示:
id<GraphViewControllerDelegate> delegate;
id代表;
在GraphViewController对核心Plot委托方法的实现中,只需让它们调用委托对这些方法的实现即可。将包围视图控制器设置为GraphViewController的代理,并通过实现这两种方法使其符合GraphViewController delegate协议。当您要在较大控制器的视图中添加子视图时,我尝试将其放入-(void)viewDidLoad在我的ViewController中:[self.graphView addSubview:graphController.view];但是,当我构建并运行时,IB中指定的视图graphView没有呈现。它只是一个白框。@Kevin:您在这里提供的信息太少,无法使用。self.graphView是什么?图形应该包含在graphView控制器的视图或子视图中。是包含graph的CPLayerHostingView的实例吗控制器视图或子视图中是否存在h?所有内容是否都已正确连接?self.graphView是graphView控制器中的视图应指向的UIView。CPLayerHostingView的一个实例位于graphView控制器中并已连接。我不确定的是如何将graphView控制器的视图分配给UIView graphView。在仔细检查CPTestApp IPhone示例应用程序时,我注意到view属性连接到CPLayerHostingView。这可能导致我的图形无法显示。考虑到UIView与CPLayerHostingView不同,你能解释一下它是如何工作的吗?@Kevin:CPLayerHostingView是UIView子类,因此它可以替代在ScatterPlot.xib接口生成器文件中,我们只需为视图控制器将用作CPLayerHostingView的视图设置类。其余的图形设置在CPTestAppScatterPlotController中完成。当您要在较大的控制器视图中添加子视图时,我尝试将我的ViewController中的-(void)viewDidLoad中的s:[self.graphView添加子视图:graphController.view];但是,当我构建并运行时,IB中指定的视图graphView没有呈现。它只是一个白框。@Kevin:您在这里提供的信息太少,无法使用。self.graphView是什么?图形应该包含在graphView控制器的视图或子视图中。是包含graph的CPLayerHostingView的实例吗控制器视图或子视图中是否存在h?所有内容是否都已正确连接?self.graphView是graphView控制器中的视图应指向的UIView。CPLayerHostingView的一个实例位于graphView控制器中并已连接。我不确定的是如何将graphView控制器的视图分配给UIView graphView。在仔细检查CPTestApp IPhone示例应用程序时,我注意到view属性连接到CPLayerHostingView。这可能导致我的图形无法显示。考虑到UIView与CPLayerHostingView不同,您能解释一下它是如何工作的吗?@Kevin:CPLayerHostingView是UIView子类,所以它