2011斯坦福大學iOS應用開發教程學習筆記(第二課)My First iOS App

第二課名稱是: My First iOS App 我的第一個iOS應用 

注意:我用的是XCode Version 4.5.2 (4G2008a)版本,SDK 是6.0,和視頻教程稍微不一樣。

這課主要是以一個計算器一個用爲例子,教你怎麼使用XCode,如何使用MVC設計模式創建應用。

我們跟着他把應用做出來,這顆學習的目的就達到了。

1、新建一個single view application模版的應用

填寫項目信息。


前綴加上 Calculator,新建的Viewcontroller前面都帶有Calculator。選擇了 使用故事版,使用ARC,這都是iOS 5.0之後的新特性。單元測試就不選了,後期纔會去學。

接下來按他的演示,在故事版的View上放下一個Label控件,並調整位置和大小,如圖:



創建outlet

好吧,老頭繼續把MVC模式的圖放了出來:


爲了說明Controller創建一個outlet到view, 如何操作呢?在Label按住上按住Control鍵,拖到.h文件,放開


要用weak指針,因爲它已經在這個窗口上,有一個strong指針指向它了。所以只需要一個weak指針就行了。


@property (weaknonatomicIBOutlet UILabel *display;

IBOutlet 這個類型沒有具體的內容,只是Xcode用來跟蹤那個 property是Outlet。 編譯器會忽略它,沒有什麼實際內容。


添加一些按鈕。

Round Rect Button。

這就使用到了MVC的target -action,


在.m文件裏生成了代碼如下:

  1. - (IBAction)digitPressed:(id)sender {  
  2. }  
其實 IBAction什麼都不是,只是讓Xcode知道這是個aciton,事實上 IBAction是個void。
消息的參數 sender ,就是按鈕自己,id是啥呢? 是個很總要的類型,可以指向任何類型對象的指針。

複製多個按鈕,複製好之後改變按鈕上的數字。


當你複製按鈕的時候,也複製了它的target- action

修改代碼:

  1. - (IBAction)digitPressed:(UIButton *)sender {  
  2.     NSString *digit = [sender currentTitle];  
  3.     NSLog(@"digit pressed = %@", digit);  
  4. }  

取得當前按下按鈕的title,並在控制檯打印出來。

運行程序看看,第一次運行比較慢,因爲要預編譯一些文件,讀入framework等,第二次就快了。

在Label 上顯示數字,添加代碼如下:

  1. - (IBAction)digitPressed:(UIButton *)sender {  
  2.     NSString *digit = [sender currentTitle];  
  3.       
  4.     UILabel *myDisplay = self.display; //[self display];  
  5.     NSString *currentText = myDisplay.text; //[myDisplay text];  
  6.     NSString *newText = [currentText stringByAppendingString:digit];  
  7.     myDisplay.text = newText; //[myDisplay setText:newText];  
  8. }  
後面註釋的是另外一種寫法,和.號的寫法一個作用。

上面的代碼可以縮減成這樣:

  1. - (IBAction)digitPressed:(UIButton *)sender {  
  2.     NSString *digit = [sender currentTitle];  
  3.     self.display.text = [self.display.text stringByAppendingString:digit];  
  4. }  

小技巧,可以按住option鍵,點擊某個方法或類,得到這個方法活類的文檔。

增加+ - * /符號的按鈕,增加Enter 按鈕,參數是None:

建立一個model

新建文件,CalculatorBrain。它是計算器的大腦

把操作壓入棧,完成棧上的操作。

@synthesize的實現getter setter的樣子。

  1. - (NSMutableArray *)operandStack  
  2. {  
  3.     if (_operandStack == nil) {  
  4.         _operandStack = [[NSMutableArray alloc] init];  
  5.     }  
  6.     return _operandStack;  
  7. }  
  8. - (void)setOperandStack:(NSMutableArray *)operandStack  
  9. {  
  10.     _operandStack = operandStack;  
  11. }  

上面operandStack的實例化是延遲實例化 ,這個方式在iOS裏經常用的。

這裏是brain的.m文件和.h文件代碼:

  1. //  
  2. //  CalculatorBrain.m  
  3. //  Calculator  
  4. //  
  5. //  Created by rongfzh on 12-11-22.  
  6. //  Copyright (c) 2012年 rongfzh. All rights reserved.  
  7. //  
  8.   
  9. #import "CalculatorBrain.h"  
  10.   
  11. @interface CalculatorBrain()  
  12. @property (nonatomic, strong) NSMutableArray *operandStack;  
  13. @end  
  14.   
  15. @implementation CalculatorBrain  
  16. @synthesize operandStack = _operandStack;  
  17.   
  18.   
  19. - (NSMutableArray *)operandStack  
  20. {  
  21.     if (_operandStack == nil) {  
  22.         _operandStack = [[NSMutableArray alloc] init];  
  23.     }  
  24.     return _operandStack;  
  25. }  
  26. - (void)setOperandStack:(NSMutableArray *)operandStack  
  27. {  
  28.     _operandStack = operandStack;  
  29. }  
  30.   
  31. - (void)pushOperand:(double)operand{  
  32.     [self.operandStack addObject:[NSNumber numberWithDouble:operand]];  
  33.       
  34. }  
  35.   
  36. -(double)popOperand  
  37. {  
  38.     NSNumber *operandObject = [self.operandStack lastObject];  
  39.     if (operandObject  != nil) {  
  40.         [self.operandStack removeLastObject];  
  41.   
  42.     }  
  43.     return  [operandObject doubleValue];  
  44. }  
  45. - (double)performOperation:(NSString *)operation{  
  46.     double result = 0;  
  47.     if ([operation isEqualToString:@"+"]) {  
  48.         result = [self popOperand] + [self popOperand];  
  49.     }  
  50.     [self pushOperand:result];  
  51.     return result;  
  52. }  
  53. @end  

  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface CalculatorBrain : NSObject  
  4. - (void)pushOperand:(double)operand;  
  5. - (double)performOperation:(NSString *)operation;  
  6. @end  
裏面的具體的解釋看老頭講解吧。主要的流程是把操作數放到棧裏,按下操作符時取出操作數進行相應的運算。這節課實現了+法。

controller的代碼實現:

  1. #import "CalculatorViewController.h"  
  2. #import "CalculatorBrain.h"  
  3.   
  4. @interface CalculatorViewController ()  
  5. @property (nonatomic) BOOL userIsInTherMiddleOfEnteringANumber;  
  6. @property (nonatomic , strong) CalculatorBrain *brain;  
  7. @end  
  8.   
  9. @implementation CalculatorViewController  
  10.   
  11. @synthesize brain = _brain;  
  12.   
  13. - (CalculatorBrain *)brain  
  14. {  
  15.     if (!_brain) {  
  16.         _brain = [[CalculatorBrain alloc] init];  
  17.     }  
  18.     return _brain;  
  19. }  
  20. - (void)viewDidLoad  
  21. {  
  22.     [super viewDidLoad];  
  23.     // Do any additional setup after loading the view, typically from a nib.  
  24. }  
  25.   
  26. - (void)didReceiveMemoryWarning  
  27. {  
  28.     [super didReceiveMemoryWarning];  
  29.     // Dispose of any resources that can be recreated.  
  30. }  
  31.   
  32. - (IBAction)digitPressed:(UIButton *)sender {  
  33.     NSString *digit = [sender currentTitle];  
  34.     if (self.userIsInTherMiddleOfEnteringANumber) {  
  35.         self.display.text = [self.display.text stringByAppendingString:digit];  
  36.     }else{  
  37.         self.display.text = digit;  
  38.         self.userIsInTherMiddleOfEnteringANumber = YES;  
  39.     }  
  40. }  
  41.   
  42. - (IBAction)operationPressed:(UIButton*)sender  
  43. {  
  44.     if (self.userIsInTherMiddleOfEnteringANumber) {  
  45.         [self enterPressed];  
  46.     }  
  47.     double result = [self.brain performOperation:sender.currentTitle];  
  48.     NSString *resultString = [NSString stringWithFormat:@"%g", result];  
  49.     self.display.text = resultString;  
  50. }  
  51.   
  52. - (IBAction)enterPressed {  
  53.     [self.brain pushOperand:[self.display.text doubleValue]];  
  54.     self.userIsInTherMiddleOfEnteringANumber = NO;  
  55. }  
  56. @end  

運行結果:


課程代碼下載:http://download.csdn.net/detail/totogo2010/4798557

容芳志 (http://blog.csdn.net/totogo2010)

本文遵循“署名-非商業用途-保持一致”創作公用協議

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章