我正在制作一个计算器,并有两个 MVC 用于数学部分,称为 CalculatorFirstViewController 和一个名为 CalculatorBrain 的类。图形部分的另一个称为 GraphViewController。
在 CalculatorController 中,我使用 mutableArray 作为计算器堆栈,并通过 segue 将其传递给图形视图控制器。GraphView 的属性称为 graphingPoints。之后通过drawrect并调用“programToGraph”方法,该方法将创建一个点数组来绘制图形。让我感到困惑的是,我在“programGraph”中调用了一个方法“runProgram:usingVariableValues”,尽管“runProgram”只在 CalculatorBrain 中声明,这是一个单独的目标文件。为什么这个方法调用有效?
@interface CalculatorFirstViewController ()
@property (nonatomic, strong) CalculatorBrain *brain;
@end
@implementation CalculatorFirstViewController
@synthesize brain = _brain;
- (CalculatorBrain*) brain{
   if(!_brain) _brain = [[CalculatorBrain alloc] init];
   return _brain;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
   if([segue.identifier isEqualToString:@"Graph"])
   //The call [self.brain program] returns the CalculatorStack
    [segue.destinationViewController setGraphingPoint:[self.brain program]];
  }
这里是 RunProgram 的声明,在 Calculator MVC 中使用的 CalculatorBrain 对象中声明。它是一个类方法。它所做的只是返回在堆栈上执行操作的值。
+ (double)runProgram:(id)program usingVariableValues:(NSDictionary*)variableValues;
这是 graphViewController 代码。
-(void) setGraphingPoint:(NSMutableArray*) graphingPoint{
if(_graphingPoint == nil) _graphingPoint = [[NSMutableArray alloc] init ];
    _graphingPoint = graphingPoint;
    //this will call drawrect
    [self.graphingView setNeedsDisplay];
}
-(id) programToGraph:(GraphingView *)sender{
    CGPoint graphPoint;
    NSMutableArray *pointValues = [[NSMutableArray alloc] init];
    for( int x =0;x<5; x++)
    {
        NSDictionary* xValue = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:x] forKey:@"X"];
        graphPoint.x =x;
     //This is the main part I dont get, If calculatorBrain is a seperate object file 
     //and I didn't import it, how am I able to call the method by just saying 
     //CalculatorBrain as the receiver? 
        graphPoint.y = [CalculatorBrain runProgram: self.graphingPoint usingVariableValues:xValue];
        [pointValues addObject:[NSValue valueWithCGPoint:graphPoint]];
}
return pointValues;
}
那么,虽然我没有导入 CalculatorBrain 文件并且我没有通过 segue 将它传递给另一个控制器,但我怎么能调用 runProgram 呢?