標籤:style blog color io os ar for strong sp
CalculatorViewController.h
//iOS> Application > Single View Application 建立一個模板controller和空白View#import <UIKit/UIKit.h>@interface CalculatorViewController : UIViewController@property (weak, nonatomic) IBOutlet UILabel *display; //display------label的,view到controller的outlet@end //為什麼用weak指標?因為label在這個視窗上,這個父視窗View已經有個strong指標指向它了 , //如果視窗沒有strong指標指向它,那我也不需要label了,因為視窗都沒了label也沒了. //所以我只需要weak指標,我只需要它在View裡,通常Outlet都是weak的
CalculatorViewController.m
#import "CalculatorViewController.h"#import "CalculatorBrain.h"@interface CalculatorViewController()@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber; //初始值是0,屬於private@property (nonatomic ,strong)CalculatorBrain *brain;@end @implementation CalculatorViewController@synthesize display=_display; //display------label的名字,view到controller的outlet,@synthesize userIsInTheMiddleOfEnteringANumber=_userIsInTheMiddleOfEnteringANumber;@synthesize brain=_brain;-(CalculatorBrain *)brain{ if (!_brain) _brain=[[CalculatorBrain alloc]init]; //Lazy instantiation return _brain;}- (IBAction)digitPressed:(UIButton *)sender //digitPressed------Round Rect Button的,target action。 //當按鈕被按,view發訊息給controller,訊息附帶參數,參數是寄件者自己,這裡就是按鈕 { //本函數作用-------按下按鈕,label變化. NSString *digit=sender.currentTitle; //也是property所以可以也應該用.號(此處是複製了NSString還是給了個 //指標指向NSString?依這個property的聲明而定,若property是strong的則是複製) if(self.userIsInTheMiddleOfEnteringANumber) { self.display.text=[self.display.text stringByAppendingString:digit]; } else { self.display.text=digit; //property的getter方法可以用.號,擷取值的方法是發getter到self的Label self.userIsInTheMiddleOfEnteringANumber=1; }}- (IBAction)OperationPressed:(UIButton *)sender{ if(self.userIsInTheMiddleOfEnteringANumber)[self enterPressed]; //"6 enter 3 enter +" VS "6 enter 3 +" double result=[self.brain performOperation:sender.currentTitle]; NSString *resultString=[NSString stringWithFormat:@"%g",result]; self.display.text=resultString;}- (IBAction)enterPressed //把運算元入棧到model裡,需要一個指向model的指標。所以幾乎每個controller { //都有這麼一個private的property,strong的,而且還需要synthesize。 [self.brain pushOperand:[self.display.text doubleValue]]; self.userIsInTheMiddleOfEnteringANumber=0; }@end
[Stanford] RPN Calculator(controller)