Stanford Cource(2)Demo App Caculator
Recall all the IOS informations.
APNS1~4
http://sillycat.iteye.com/blog/1769158
http://sillycat.iteye.com/blog/1769163
http://sillycat.iteye.com/blog/1769781
http://sillycat.iteye.com/blog/1849217
Envrionment 1~2
http://sillycat.iteye.com/blog/1286175
http://sillycat.iteye.com/blog/1286178
Stanford Cource 1
http://sillycat.iteye.com/blog/1747763
Label, button and a lot of widgets which like VB to me.
Controller will take to the Label in View Ctrl + drag ----> Outlet ----> name display
Button in the View send message to the Controller Ctrl + drag -----> Target ----> digit press
When we copy and paste the button, we copy all the Target.
id is very important in Object-C, it can point to any object. Any Object can be id.
- (IBAction)digitpressed:(UIButton *)sender
{
NSString *digit = sender.currentTitle;
…snip…
}
local variable, NSString
Put console log
NSLog(@"Digit pressed = %@", digit);
UILabel *myDisplay = self.display;
A pointer to the UILabel
NSString *currentText = myDisplay.text;
Get the text of the display
Option + click can give us tip for help.
NSString *newText = [currentText stringByAppendingString:digit];
myDisplay.text = newText;
NSString append the string, and myDisplay call the setText method to update the appending new digit.
Private property in the implementation
@interface CalculatorViewController () @property (nonatomic) BOOL userIsInTheMiddleOfEnterANumber; @property (nonatomic, strong) CalculatorBrain *brain;
@end
Use a local Array in Object C
@interface CalculatorBrain() @property (nonatomic, strong) NSMutableArray *operandStack; @end@implementation CalculatorBrain
@synthesize operandStack = _operandStack;
Add object to Array
[self.operandStack addObject:operandObject];
Make a double number an object
NSNumber *operandObject = [NSNumber numberWithDouble:operand];
Make sure the stack/array is not nil in the getter
-(NSMutableArray *)operandStack{ if(_operandStack == nil){ _operandStack = [[NSMutableArray alloc] init]; } return_operandStack;
}
Get the last digit of the stack
NSNumber *operandObject = self.operandStack.lastObject;if(operandObject){ //check if there is at least one object there [self.operandStack removeLastObject];}
return operandObject.doubleValue;
NSString equal
if([operation isEqualToString:@"+"]){ result = [self popOperand] + [self popOperand];}elseif ([operation isEqualToString:@"-"]){ result = [self popOperand] - [self popOperand];}elseif ([operation isEqualToString: @"*"]){ result = [self popOperand] * [self popOperand];}elseif([operation isEqualToString: @"/"]){ result = [self popOperand] / [self popOperand];
}
Try to convert the String to doubleValue
[self.brain pushOperand:[self.display.text doubleValue]];
References:
http://www.cocoachina.com/iphonedev/toolthain/2011/1019/3387.html
http://tangqiaoboy.blog.163.com/blog/static/11611425820111013102549814/
Stanford Cource(2)Demo App Caculator
最新推荐文章于 2020-08-20 11:47:00 发布