如果 GraphView.m 是一个 UIView 你不会走得太远,就像performSegueWithIdentifier:sender
UIViewController 方法一样。
假设 graphView 是从 viewController 创建的,您希望将其设置为 viewController 的委托。
在 GraphView.h
在 @interface 上方声明一个 graphView 协议:
@protocol GraphViewDelegate
-(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index
@end
声明一个属性:
@property (weak) id <GraphViewDelegate> delegate;
在 GraphView.m 中:
-(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index
{
[[self delegate] barPlot:plot barWasSelectedAtRecordIndex:index];
}
在 ViewController.h 中修改 @interface 行
@interface MyViewController: UIViewController <GraphViewDelegate>
在 ViewController.m
when you create the graphView, set the delegate to self (if graphView is created in Interface Builder, you can CTRL-drag a line to the viewController to set the delegate):
GraphView* graphView = [GraphView alloc] init];
[graphView setDelegate:self];
implement the delegate method as you had it in GraphView (you may not need the index
parameter but I carried it over anyway)..
-(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index
{
[self performSegueWithIdentifier:@"list2" sender:self];
}
You probably want to modify your method signature, to something like
-(void)graphView:(GraphView*)graphView
didSelectBarAtIndex:(NSUInteger)index
barPlot:(CPTBarPlot *)plot
(it's good practice to send a reference to the sender along with the message)