我正在製作一個計算器,並且有一個稱爲CalculatorBrain的類,名爲CalculatorFirstViewController的數學部分有兩個MVC。圖表部分的另一個稱爲GraphViewController。作品,但我不知道爲什麼,目標c
在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;
}
那麼怎麼來的,我可以打電話runProgram雖然我沒有導入CalculatorBrain文件,我沒有通過SEGUE傳遞到另一個控制器?
你意識到你必須在m或h中導入這個東西,因爲你可以訪問CalculatorBrain類。或者你有一個前向引用(@class),但是然後CLANG會抱怨它。 – CodaFi 2012-07-22 22:56:07
我沒有將任何CalculatorFiles導入到graphingController中,但我沒有將CalculatorController導入到CalculatorControlelr中以便能夠調用Segue。我以爲你必須保持MVC分開 – 2012-07-22 23:21:53
你有沒有什麼課程? – CodaFi 2012-07-22 23:23:39