UIView
經過前幾天的快速學習,我們初步了解的IOS開發的一些知識,中間因為拉的太急,忽略了很多基礎知識點,這些知識點單獨拿出來學習太過枯燥,我們在今后的項目中再逐步補齊,今天我們來學習APP視圖相關知識。
視圖即UIView對象,我們上次用的按鈕UIButton、UILabel或者UITableView皆是其子類;視圖知道如何繪制自己與前端一致有一個層次的概念。
任何一個應用都會有一個UIWindow對象,與瀏覽器對象一致,他作為容器角色而存在,負責裝載所有的視圖控件,每個加入的視圖便是一個子視圖:subview,視圖嵌套便形成了我們看到的APP界面,這點類似與html的dom結構。
視圖的繪制分為兩步:
① 層次結構上的每個視圖(包括根視圖UIWindow)分別繪制自身,視圖將自己繪制到圖層(layer)上,每個UIView都擁有一個layer屬性,指向一個CALayer對象
② 所有視圖的圖像最終組成為一幅圖像,繪制到屏幕上
為了更好的了解UIView的相關知識,我們這里做一個簡單的計算器來說明其知識點吧。
其實說明這個知識點我可以選擇很多項目之所以會選擇計算器是因為內部會碰到數字與字符串轉換,四則運算問題,也會遇到控件綁定事件等行為,幫助我們鞏固基礎吧。
這里新建一個單頁應用:Calculator-app,自從昨天我知道了拖控件居然需要以拖動的方式建立關聯后,我便決定不再使用拖的方式,我們這里使用代碼生成界面,所以今天的目的是:
① 了解OC的數據類型轉換
② 了解UIView的知識
③ 了解如何代碼構建視圖並且綁定事件
至於計算器的邏輯,不必太過在意
畫圓
首先,我們在我們的UIView中畫一個同心圓來了解UIView的相關知識,因為OC並沒有提供繪制同心圓的視圖對象,該知識點可以幫助我們更好的了解UIView。
新建一個UIView的子類:MyUICircle
每個UIView的子類有兩個方法需要我們關注,第一個為初始化方法:
MyUICircle *circle = [MyUICircle alloc] initWithFrame:<#(CGRect)#>
其中initWithFrame是繼承下來的初始化方法,帶有一個CGRect類型的參數,該參數會賦予UIView的frame屬性:
@property (nonatomic) CGRect frame;
CGRect包含另外兩個結構,origin=>x, y; size=>width, height。四個都是float的基本結構,我們在全局控制器中試試其作用:
1 #import "ViewController.h" 2 #import "MyUICircle.h" 3 4 @interface ViewController () 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 //因為CGRect不是OC對象,所以不能接收OC的消息,也沒有必要接收消息,一般這樣建立一個OCRect對象 14 //注意,這里不是指針 15 CGRect frame = CGRectMake(10, 10, 100, 100); 16 17 //創建基本視圖對象 18 //這里已經確定好了坐標與尺寸 19 MyUICircle *circle = [[MyUICircle alloc] initWithFrame:frame]; 20 21 //給一個背景,這里使用了顏色類的靜態方法 22 circle.backgroundColor = [UIColor redColor]; 23 24 //將視圖加入主視圖中 25 [self.view addSubview:circle]; 26 27 } 28 29 - (void)didReceiveMemoryWarning { 30 [super didReceiveMemoryWarning]; 31 // Dispose of any resources that can be recreated. 32 } 33 34 @end
因為MyUICircle是一個空類,什么都沒有做便沒有貼代碼了,這個是視圖對應的主控制器,其與視圖如何建立關聯我們暫不關注,他提供一個方法viewDidLoad處理視圖准備好的初始化動作,我們便將創建好的視圖加入了其中,大家請看這塊紅斑:

這個時候的層次結構是
UIWIndow包含多個UIView,這里主UIView還包含一個MyUICircle對象,每個UIView可以根據superview屬性找到自己的父對象
drawRect
drawRect為從UIView繼承下來的第二個需要關注的方法,他負責在頁面上繪圖,我們可以重寫drawRect達到自定義繪圖的功能。
1 // Only override drawRect: if you perform custom drawing. 2 // An empty implementation adversely affects performance during animation. 3 - (void)drawRect:(CGRect)rect { 4 // Drawing code 5 }
UIView的子類具有兩個相似的屬性,bounds與frame,我們都知道frame是用於視圖本身布局用的,而bounds定義了一個矩形范圍,表示視圖的繪制區域。
視圖在繪制時,會參考一個坐標系,bounds表示矩形位於自己的坐標系,而frame表示矩形位於父視圖的坐標系,兩個矩形大小是相等的。
1 -(CGRect)frame{ 2 return CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height); 3 } 4 -(CGRect)bounds{ 5 return CGRectMake(0,0,self.frame.size.width,self.frame.size.height); 6 }
bounds是相對於左上角位置的,不太好理解就寫demo,我這里新建了一個子View幫助理解
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 //因為CGRect不是OC對象,所以不能接收OC的消息,也沒有必要接收消息,一般這樣建立一個OCRect對象 5 //注意,這里不是指針 6 CGRect frame = CGRectMake(50, 50, 100, 100); 7 8 //創建基本視圖對象 9 //這里已經確定好了坐標與尺寸 10 MyUICircle *circle = [[MyUICircle alloc] initWithFrame:frame]; 11 12 //給一個背景,這里使用了顏色類的靜態方法 13 circle.backgroundColor = [UIColor redColor]; 14 15 //circle.bounds = CGRectMake(200, 200, 100, 100); 16 17 UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)]; 18 view2.backgroundColor = [UIColor yellowColor]; 19 [circle addSubview: view2]; 20 21 //將視圖加入主視圖中 22 [self.view addSubview:circle]; 23 24 }

可以看到,子view是相對於父View布局的,這里與我們的absolute一致,如果改變父View的bounds呢:
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 //因為CGRect不是OC對象,所以不能接收OC的消息,也沒有必要接收消息,一般這樣建立一個OCRect對象 5 //注意,這里不是指針 6 CGRect frame = CGRectMake(50, 50, 100, 100); 7 8 //創建基本視圖對象 9 //這里已經確定好了坐標與尺寸 10 MyUICircle *circle = [[MyUICircle alloc] initWithFrame:frame]; 11 12 //給一個背景,這里使用了顏色類的靜態方法 13 circle.backgroundColor = [UIColor redColor]; 14 15 //父類設置該參數造成的影響 16 circle.bounds = CGRectMake(10, 0, 100, 100); 17 18 UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 50, 50)]; 19 view2.backgroundColor = [UIColor yellowColor]; 20 [circle addSubview: view2]; 21 22 //與view2參照物 23 UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(20, 200, 50, 50)]; 24 view3.backgroundColor = [UIColor grayColor]; 25 [self.view addSubview: view3]; 26 27 //將視圖加入主視圖中 28 [self.view addSubview:circle]; 29 30 }

可以看到,view2相對於父元素最初位置左移了10像素,所以修改了一個view的bounds邊上修改了本地坐標的原點位置,最初的原點是0, 0。
那里view1修改了原點坐標讓其右偏移了10像素,所以對應的子view便需要左偏,我理解下來是設置后將當前右上角的坐標變成了10像素,大概是這樣吧。
這里為什么要強調這個bounds本地坐標呢,因為以后拖動相關的需求都會根據他做計算,這塊有點繞,我們后續真實項目遇到再來糾結他吧,現在也說不清。
正式畫圓
如果我們期待畫一個鋪滿屏幕最大的圓,便需要通過bounds屬性找到中心點:
1 - (void)drawRect:(CGRect)rect { 2 3 CGRect bounds = self.bounds; 4 5 //計算中心點 6 CGPoint center; 7 center.x = bounds.origin.x + bounds.size.width / 2.0; 8 center.y = bounds.origin.y + bounds.size.height / 2.0; 9 10 //根據寬高計算較小值的半徑 11 float radius = (MIN(bounds.size.width, bounds.size.height) / 2.0); 12 13 }
有了中心點與半徑便可以畫圓了,這里使用UIBezierPath類繪制圓形:
1 - (void)drawRect:(CGRect)rect { 2 3 CGRect bounds = self.bounds; 4 5 //計算中心點 6 CGPoint center; 7 center.x = bounds.origin.x + bounds.size.width / 2.0; 8 center.y = bounds.origin.y + bounds.size.height / 2.0; 9 10 //根據寬高計算較小值的半徑 11 float radius = (MIN(bounds.size.width, bounds.size.height) / 2.0); 12 13 UIBezierPath *path = [[UIBezierPath alloc] init]; 14 15 //這里去看api吧,他們都說ios門檻高,原來什么都是英文啊 16 [path addArcWithCenter:center radius:radius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES]; 17 18 //開始繪制 19 [path stroke]; 20 }

於是到此圓基本就出來了,至此我們對UIView有了一個基本的了解,接下來就讓我們開始真實的計算器邏輯了吧
計算器
最簡單的計算器擁有10個數字,+-*/,小數點,重置鍵,一個顯示結果的文本框,並不復雜,我們這里便使用代碼設計其UI。
首先,我們將數字畫出來,根據前端的經驗,數字按鈕肯定是一個循環組成,dom上會留下一些標識,並且給父元素綁定事件即可,但是ios我們並不熟悉,所以先用最土的方法是畫10個數字,在此之前,我們先簡單研究下UIButton控件。
UIButton
這一小節參考了:http://justcoding.iteye.com/blog/1467999
UIButton是一個標准的UIController,所謂UI控件便是對UIView的的增強,也就是說UI控件一般繼承至UIView,所以View的一些統一特性都被繼承了下來了。
繼承下來的屬性
enabled,控件是否可用,與html控件一致,禁用后仍然可見,只是不可點擊。
selected,當用戶選中控件時,控件的selected屬性便為YES。
contentVerticalAlignment,控件在垂直方向布置自身內容的方式,以下是可設置的值:
① UIControlContentVerticalAlignmentCenter
② UIControlContentVerticalAlignmentTop
③ UIControlContentVerticalAlignmentBottom
④ UIControlContentVerticalAlignmentFill
contentHorizontalAligment,為水平對齊的方式,可設置的值為:
① UIControlContentHorizontalAlignmentCenter
② UIControlContentHorizontalAlignmentTop
③ UIControlContentHorizontalAlignmentBottom
④ UIControlContentHorizontalAlignmentFill
事件通知
UIControll提供一個標准機制,來進行事件訂閱和發布,當控件觸發特定事件后,便會觸發對應回調。
[myControl addTarget: myDelegate action:@selector(myActionmethod:) forControlEvents:UIControlEventValueChanged ];
可以使用addTarget綁定多個事件,這類事件一般來說都是標准的,比如js中的click事件,move等事件
UIControlEventTouchDown,點擊事件,類似與js中的click
UIControlEventTouchDownRepeat,當點擊數大於1時觸發,類似doubleclick
UIControlEventTouchDraglnside,當觸摸控件在窗口滾動時觸發
UIControlEventTouchChanged,當控件的值發生變化時觸發
UIControlEventEditingDidBegin,當開始編輯時觸發,類似獲取焦點吧
UIControlEventEditingChanged,當文本控件中的文本被改變時發送通知。
UIControlEventEditingDidEnd,當文本控件中編輯結束時發送通知。
......
與addTarget方法對應的是removeTarget,表達的是刪除事件
使用allTargets獲取一個控件的所有事件列表
之前是關於UIButton父類的事件,這里我們回到UIButton,首先說下兩種初始化方法:
initWithFrame
1 UIButton *tmp; 2 CGRect frame; 3 4 frame = CGRectMake(50, 50, 100, 40); 5 tmp = [[UIButton alloc] initWithFrame:frame];
buttonWithType
這個是UIButton一個特有的類方法
1 UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 2 3 typedef enum { 4 UIButtonTypeCustom = 0, // no button type 自定義,無風格 5 UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card 白色圓角矩形,類似偏好設置表格單元或者地址簿卡片 6 UIButtonTypeDetailDisclosure,//藍色的披露按鈕,可放在任何文字旁 7 UIButtonTypeInfoLight,//微件(widget)使用的小圓圈信息按鈕,可以放在任何文字旁 8 UIButtonTypeInfoDark,//白色背景下使用的深色圓圈信息按鈕 9 UIButtonTypeContactAdd,//藍色加號(+)按鈕,可以放在任何文字旁 10 } UIButtonType;
屬性
這種方法創建是沒有位置信息的,所以需要設置frame值:
CGRect btn2Frame = CGRectMake(10.0, 10.0, 60.0, 44.0); btn2.frame =btn2Frame;
除此之外,我們需要設置按鈕的文字,采用setTitle,帶一個字符串和一個當前按鈕的狀態:
[btn1 setTitle:@"BTN1" forState:UIControlStateNormal];
我們也可以使用一個圖片作為按鈕:
[btn2 setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal];
最后可以為每種按鈕設置標題的顏色和陰影,以及背景,
[btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//設置標題顏色 [btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];//陰影 [btn1 setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted];//背景圖像
forstate決定了按鈕將在何種狀態下顯示:
enum { UIControlStateNormal = 0, //常態 UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set 高亮 UIControlStateDisabled = 1 << 1, //禁用 UIControlStateSelected = 1 << 2, // flag usable by app (see below) 選中 UIControlStateApplication = 0x00FF0000, // additional flags available for application use 當應用程序標志使用時 UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use 為內部框架預留的 }; typedef NSUInteger UIControlState;
當按鈕高亮或者禁用,UIButton可以調整自己的樣式
添加事件(動作)
按鈕還是需要綁定事件,他的事件綁定直接繼承自UIView:
-(void)btnPressed:(id)sender{ UIButton* btn = (UIButton*)sender; //開始寫你自己的動作 }
[btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
這里我們再次回到計算器,我們首先生成數字的UI,然后為數字綁定事件,點擊每個按鈕在后台打印出數字:
1 - (void)onNumClick:(id)sender 2 { 3 self.msg.text = [sender currentTitle]; 4 } 5 6 - (void)viewDidLoad { 7 [super viewDidLoad]; 8 9 //生成10個數字先,一行4個,一字排開 10 UIButton *tmp; 11 CGRect frame; 12 self.msg = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 100, 40)]; 13 [self.view addSubview:self.msg]; 14 15 self.msg.text = @"數字"; 16 17 for (int i = 0; i < 10; i++) { 18 frame = CGRectMake(50, 50 + 50 * i, 80, 40); 19 tmp = [[UIButton alloc] initWithFrame:frame]; 20 [tmp setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal]; 21 22 //不設置背景就是白色就看不到啦 23 tmp.backgroundColor = [UIColor grayColor]; 24 25 //這里綁定事件,點擊每個數字便重置msg 26 [tmp addTarget:self action:@selector(onNumClick:) forControlEvents:UIControlEventTouchDown]; 27 28 [self.view addSubview:tmp]; 29 } 30 }
如此一來,我們簡陋的界面就出來了:

其中事件綁定的回調函數的sender參數,應該對應js函數中的e,在此我們知道了如何布局以及綁定事件,於是我們將界面稍微美化點,因為我這里用到了OC的字典,這里先插一段字典的知識吧。
NSDictionary
NSDictionary對應JS中的對象字面量,自從json對象出來后,這個對象非常重要,估計是逃不掉的啦。
NSDictionary代表不可變字典,意思是一旦初始化結束就不能增刪元素了,這是其初始化的方法:
1 //創建多個字典 2 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: 3 @"value1", @"key1", 4 @"value2", @"key2", 5 @"value3", @"key3", 6 @"value4", @"key4", 7 nil]; 8 9 //根據現有的字典創建字典 10 NSDictionary *dic2 = [NSDictionary dictionaryWithDictionary:dic]; 11 NSLog(@"dic2 :%@", dic2); 12 13 //根據key獲取value 14 NSLog(@"key3 value :%@", [dic objectForKey:@"key3"]); 15 16 //獲取字典數量 17 NSLog(@"dic count :%d", [dic count]); 18 19 //所有的鍵集合 20 NSArray *keys = [dic allKeys]; 21 NSLog(@"keys :%@", keys); 22 23 //所有值集合 24 NSArray *values = [dic allValues]; 25 NSLog(@"values :%@", values);
2015-08-16 17:52:35.540 Calculator-app[1266:121201] dic2 :{ key1 = value1; key2 = value2; key3 = value3; key4 = value4; } 2015-08-16 17:52:35.540 Calculator-app[1266:121201] key3 value :value3 2015-08-16 17:52:35.540 Calculator-app[1266:121201] dic count :4 2015-08-16 17:52:35.541 Calculator-app[1266:121201] keys :( key3, key1, key4, key2 ) 2015-08-16 17:52:35.541 Calculator-app[1266:121201] values :( value3, value1, value4, value2 )
NSMutableDictionay
NSMutableDictionary是NSDictionary的子類,所以繼承了NSDictionary的所有特性,並且可以使用方法動態添加:
[dictionary setObject:@"value" forKey:@"key"];
這里我們回到我們的計算器,因為我們知道到底有多少字符,所以這里直接使用不可變字典即可,但是最后轉念一想好像數組也是可行的,所以就數組吧......
因為這里是學習ios開發,各位就不要在意小數點之類的細節了,至此基本UI便出來了,雖然很丑:
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 5 @property(retain,nonatomic) UILabel *msg; 6 7 -(void) initLayout; 8 9 @end 10 11 #import "ViewController.h" 12 #import "MyUICircle.h" 13 14 @interface ViewController () 15 16 @end 17 18 @implementation ViewController 19 20 //計算機布局 21 //思考,感覺這里沒有做到很好的分離 22 -(void) initLayout{ 23 //生成10個數字先,一行4個,一字排開 24 UIButton *tmp; 25 CGRect frame; 26 self.msg = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 40)]; 27 [self.view addSubview:self.msg]; 28 self.msg.text = @"數字"; 29 30 NSMutableArray *array = [[NSMutableArray alloc] init]; 31 32 //插入5個數字,這里發生了裝箱 33 for (int i = 0; i < 10; i++) { 34 NSString *n = [NSString stringWithFormat:@"%d", i]; 35 [array addObject:n]; 36 } 37 38 [array addObject: @"+"]; 39 [array addObject: @"-"]; 40 [array addObject: @"*"]; 41 [array addObject: @"/"]; 42 [array addObject: @"="]; 43 [array addObject: @"c"]; 44 45 for(int i = 0; i < 5; i++) { 46 for(int j = 0; j < 4; j++){ 47 if(i * 4 + j > [array count] - 1) break; 48 49 frame = CGRectMake(10 + j * 60, 100 + 60 * i, 50, 50); 50 51 tmp = [[UIButton alloc] initWithFrame:frame]; 52 [tmp setTitle: [array objectAtIndex:(i * 4 + j)] forState:UIControlStateNormal]; 53 54 //不設置背景就是白色就看不到啦 55 tmp.backgroundColor = [UIColor grayColor]; 56 57 //這里綁定事件,點擊每個數字便重置msg 58 [tmp addTarget:self action:@selector(onNumClick:) forControlEvents:UIControlEventTouchDown]; 59 60 [self.view addSubview:tmp]; 61 62 } 63 } 64 } 65 66 - (void)onNumClick:(id)sender { 67 self.msg.text = [sender currentTitle]; 68 } 69 70 - (void)viewDidLoad { 71 [super viewDidLoad]; 72 [self initLayout]; 73 } 74 75 - (void)didReceiveMemoryWarning { 76 [super didReceiveMemoryWarning]; 77 // Dispose of any resources that can be recreated. 78 } 79 80 @end

這個時候我們來簡單寫一下其業務邏輯,邏輯肯定有問題,簡單看功能就好:
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 5 @property(retain,nonatomic) UILabel *msg; 6 7 //存取字符串顯示的字符串 8 @property(retain,nonatomic) NSMutableString *resultStr; 9 10 //存儲最近一次有效的指令 11 @property(retain,nonatomic) NSMutableString *commondStr; 12 13 @property(assign, nonatomic) int num1; 14 @property(assign,nonatomic) int num2; 15 16 -(void) initLayout; 17 18 -(void) calculatorNum; 19 20 @end 21 22 23 #import "ViewController.h" 24 #import "MyUICircle.h" 25 26 @interface ViewController () 27 28 @end 29 30 @implementation ViewController 31 32 //計算機布局 33 //思考,感覺這里沒有做到很好的分離 34 -(void) initLayout{ 35 //生成10個數字先,一行4個,一字排開 36 UIButton *tmp; 37 CGRect frame; 38 self.msg = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 40)]; 39 [self.view addSubview:self.msg]; 40 41 _resultStr = [[NSMutableString alloc]initWithString:@"0"]; 42 _commondStr = [[NSMutableString alloc]initWithString:@""]; 43 44 self.msg.text = _resultStr; 45 46 NSMutableArray *array = [[NSMutableArray alloc] init]; 47 48 //插入5個數字,這里發生了裝箱 49 for (int i = 0; i < 10; i++) { 50 NSString *n = [NSString stringWithFormat:@"%d", i]; 51 [array addObject:n]; 52 } 53 54 [array addObject: @"+"]; 55 [array addObject: @"-"]; 56 [array addObject: @"*"]; 57 [array addObject: @"/"]; 58 [array addObject: @"="]; 59 [array addObject: @"c"]; 60 61 for(int i = 0; i < 5; i++) { 62 for(int j = 0; j < 4; j++){ 63 if(i * 4 + j > [array count] - 1) break; 64 65 frame = CGRectMake(10 + j * 60, 100 + 60 * i, 50, 50); 66 67 tmp = [[UIButton alloc] initWithFrame:frame]; 68 [tmp setTitle: [array objectAtIndex:(i * 4 + j)] forState:UIControlStateNormal]; 69 70 //不設置背景就是白色就看不到啦 71 tmp.backgroundColor = [UIColor grayColor]; 72 73 //這里綁定事件,點擊每個數字便重置msg 74 [tmp addTarget:self action:@selector(onNumClick:) forControlEvents:UIControlEventTouchDown]; 75 76 [self.view addSubview:tmp]; 77 78 } 79 } 80 } 81 82 - (void)onNumClick:(id)sender { 83 84 NSString *tmp = [sender currentTitle]; 85 int flag = 0; 86 87 //此處監控輸入,適合使用switch 88 if([tmp isEqualToString:@"c"]){ 89 _resultStr = [[NSMutableString alloc]initWithString:@"0"]; 90 //點擊數字則重置 91 _commondStr = [[NSMutableString alloc]initWithString:@""]; 92 93 } else if ([tmp isEqualToString:@"+"] || [tmp isEqualToString:@"-"] || [tmp isEqualToString:@"*"] || [tmp isEqualToString:@"/"]){ 94 flag = 1; 95 //如果之前已經有了命令,需要先顯示計算結果,再給予新的命令 96 if([_commondStr isEqualToString:@""]) { 97 _commondStr = tmp; 98 } else { 99 _num2 = [_resultStr intValue]; 100 [self calculatorNum ]; 101 _num1 = 0; 102 _num2 = 0; 103 } 104 105 } else if ([tmp isEqualToString:@"="]){ 106 _num2 = [_resultStr intValue]; 107 [self calculatorNum ]; 108 _num1 = 0; 109 _num2 = 0; 110 } else { 111 //這個時候是輸入數字的情況 112 if([_resultStr isEqualToString:@"0"]) { 113 _resultStr = [[NSMutableString alloc]initWithString:@""]; 114 } 115 [_resultStr appendString:tmp]; 116 } 117 118 //第一次輸入命令,便記錄為第一個數字 119 if(flag == 1) { 120 _num1 = [_resultStr intValue]; 121 _resultStr = [[NSMutableString alloc]initWithString:@""]; 122 } 123 124 self.msg.text = _resultStr; 125 126 } 127 128 - (void)calculatorNum { 129 int r; 130 if ([_commondStr isEqualToString:@"+"]){ 131 r = _num1 + _num2; 132 } else if([_commondStr isEqualToString:@"-"]) { 133 r = _num1 - _num2; 134 } else if([_commondStr isEqualToString:@"*"]) { 135 r = _num1 * _num2; 136 } else if([_commondStr isEqualToString:@"/"]) { 137 //不要在意那些邏輯了 138 r = _num1 / _num2; 139 } 140 141 _commondStr = [[NSMutableString alloc]initWithString:@""]; 142 _resultStr = [NSString stringWithFormat:@"%d", r]; 143 144 } 145 146 147 - (void)viewDidLoad { 148 [super viewDidLoad]; 149 [self initLayout]; 150 } 151 152 - (void)didReceiveMemoryWarning { 153 [super didReceiveMemoryWarning]; 154 // Dispose of any resources that can be recreated. 155 } 156 157 @end
