[Stanford] RPN Calculator(controller)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.