今天做了个iphone的小例子计算器:才用mvc设计模式
项目目录:
代码如下:
CalculatorViewController:(MVC的控制器)
// CalculatorViewController.h // Calculator // // Created by WildCat on 13-7-28. // Copyright (c) 2013年 wildcat. All rights reserved. // #import <UIKit/UIKit.h> @interface CalculatorViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *display; @end
// // CalculatorViewController.m // Calculator // // Created by WildCat on 13-7-28. // Copyright (c) 2013年 wildcat. All rights reserved. // #import "CalculatorViewController.h" #import "CalculatorBrain.h" @interface CalculatorViewController() @property (nonatomic) BOOL userIsinTheMiddleOfEnteringANumber; @property(nonatomic,strong)CalculatorBrain *brain; @end @implementation CalculatorViewController @synthesize display=_display; @synthesize userIsinTheMiddleOfEnteringANumber=_userIsinTheMiddleOfEnteringANumber; @synthesize brain=_brain; -(CalculatorBrain *)brain{ if(!_brain) _brain=[[CalculatorBrain alloc] init]; return _brain; } - (IBAction)digitPressed:(UIButton *)sender { NSString *digit=[sender currentTitle]; //建一个局部变量 if (self.userIsinTheMiddleOfEnteringANumber) { self.display.text=[self.display.text stringByAppendingString:digit]; }else{ self.display.text=digit; self.userIsinTheMiddleOfEnteringANumber=YES; } } - (IBAction)operationPressed:(UIButton *)sender { if ([self userIsinTheMiddleOfEnteringANumber]) { [self enterPressed]; } double result=[self.brain performOperation:sender.currentTitle]; NSString *resultString=[NSString stringWithFormat:@"%g",result]; self.display.text=resultString; } - (IBAction)enterPressed { [self.brain pushOperand:[self.display.text doubleValue]]; self.userIsinTheMiddleOfEnteringANumber=NO; } - (IBAction)enterDEL:(id)sender { NSString *nowstr=self.display.text; int len=nowstr.length; int nowlength=len-1; if (nowlength>=1) { self.display.text=[nowstr substringWithRange:NSMakeRange(0, nowlength)]; }else{ self.display.text=@"0"; } } @end
CalculatorBrain(计算器大脑mvc中的model)
// CalculatorBrain.h // Calculator // // Created by WildCat on 13-7-28. // Copyright (c) 2013年 wildcat. All rights reserved. // #import <Foundation/Foundation.h> @interface CalculatorBrain : NSObject -(void)pushOperand:(double)operand; -(double)performOperation:(NSString *)operation; @property (readonly)id program; +(double) runProgram:(id) program; +(NSString *)discriptionOfProgram:(id) program; @end
// // CalculatorBrain.m // Calculator // // Created by WildCat on 13-7-28. // Copyright (c) 2013年 wildcat. All rights reserved. // #import "CalculatorBrain.h" //定义一个私有的栈 @interface CalculatorBrain() @property (nonatomic,strong)NSMutableArray *programStack; @end @implementation CalculatorBrain @synthesize programStack=_programStack; //ProgramStack的setter and getter............ -(NSMutableArray *)programStack{ if (_programStack==nil) { _programStack=[[NSMutableArray alloc] init]; } return _programStack; } -(void)setProgramStack:(NSMutableArray *)programStack{ _programStack=programStack; } -(double)performOperation:(NSString *)operation{ [self.programStack addObject:operation]; return [CalculatorBrain runProgram:self.program]; } -(id)program{ return [self.programStack copy]; } //入栈 -(void)pushOperand:(double)operand{ [self.programStack addObject:[NSNumber numberWithDouble:operand]]; } //出栈 -(double)popOperand{ NSNumber *number=[self.programStack lastObject]; if (number) { [self.programStack removeLastObject]; } return [number doubleValue]; } +(double)popOperandOffStack:(NSMutableArray *)stack{ double result=0; id topOfStack=[stack lastObject]; if (topOfStack) { [stack removeLastObject]; } if ([topOfStack isKindOfClass:[NSNumber class]]){ result=[topOfStack doubleValue]; }else if ([topOfStack isKindOfClass:[NSString class]]){ NSString*operation=topOfStack; if ([@"+" isEqualToString:operation]) { result=[self popOperandOffStack:stack]+[self popOperandOffStack:stack]; }else if ([@"*" isEqualToString:operation]){ result=[self popOperandOffStack:stack]*[self popOperandOffStack:stack]; }else if ([@"-" isEqualToString:operation]){ double firstnumber=[self popOperandOffStack:stack]; double secondnumber=[self popOperandOffStack:stack]; result=secondnumber-firstnumber; }else if([@"/" isEqualToString:operation]){ double firstnumber=[self popOperandOffStack:stack]; double secondnumber=[self popOperandOffStack:stack]; if(firstnumber)result=secondnumber/firstnumber; } } return result; } +(double) runProgram:(id) program{ NSMutableArray *stack; if ([program isKindOfClass:[NSArray class]]) { stack=[program mutableCopy]; } return [self popOperandOffStack:stack]; } +(NSString *)discriptionOfProgram:(id) program{ return @"Implament this in Assignment2"; } @end
效果图: