2011斯坦福大学iOS应用开发教程学习笔记(第二课)My First iOS App
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
第二課名稱是: My First iOS App 我的第一個iOS應(yīng)用?
注意:我用的是XCode?Version 4.5.2 (4G2008a)版本,SDK 是6.0,和視頻教程稍微不一樣。
這課主要是以一個計算器一個用為例子,教你怎么使用XCode,如何使用MVC設(shè)計模式創(chuàng)建應(yīng)用。
我們跟著他把應(yīng)用做出來,這顆學(xué)習(xí)的目的就達到了。
1、新建一個single view application模版的應(yīng)用
填寫項目信息。
前綴加上 Calculator,新建的Viewcontroller前面都帶有Calculator。選擇了 使用故事版,使用ARC,這都是iOS 5.0之后的新特性。單元測試就不選了,后期才會去學(xué)。
接下來按他的演示,在故事版的View上放下一個Label控件,并調(diào)整位置和大小,如圖:
創(chuàng)建outlet
好吧,老頭繼續(xù)把MVC模式的圖放了出來:
為了說明Controller創(chuàng)建一個outlet到view, 如何操作呢?在Label按住上按住Control鍵,拖到.h文件,放開
要用weak指針,因為它已經(jīng)在這個窗口上,有一個strong指針指向它了。所以只需要一個weak指針就行了。
@property (weak, nonatomic) IBOutlet UILabel *display;
IBOutlet 這個類型沒有具體的內(nèi)容,只是Xcode用來跟蹤那個 property是Outlet。 編譯器會忽略它,沒有什么實際內(nèi)容。
添加一些按鈕。
Round Rect Button。
這就使用到了MVC的target -action,
在.m文件里生成了代碼如下:
- (IBAction)digitPressed:(id)sender { }其實 IBAction什么都不是,只是讓Xcode知道這是個aciton,事實上?IBAction是個void。消息的參數(shù) sender ,就是按鈕自己,id是啥呢? 是個很總要的類型,可以指向任何類型對象的指針。
復(fù)制多個按鈕,復(fù)制好之后改變按鈕上的數(shù)字。
當(dāng)你復(fù)制按鈕的時候,也復(fù)制了它的target- action
修改代碼:
- (IBAction)digitPressed:(UIButton *)sender {NSString *digit = [sender currentTitle];NSLog(@"digit pressed = %@", digit); }取得當(dāng)前按下按鈕的title,并在控制臺打印出來。
運行程序看看,第一次運行比較慢,因為要預(yù)編譯一些文件,讀入framework等,第二次就快了。
在Label 上顯示數(shù)字,添加代碼如下:
- (IBAction)digitPressed:(UIButton *)sender {NSString *digit = [sender currentTitle];UILabel *myDisplay = self.display; //[self display];NSString *currentText = myDisplay.text; //[myDisplay text];NSString *newText = [currentText stringByAppendingString:digit];myDisplay.text = newText; //[myDisplay setText:newText]; }后面注釋的是另外一種寫法,和.號的寫法一個作用。上面的代碼可以縮減成這樣:
- (IBAction)digitPressed:(UIButton *)sender {NSString *digit = [sender currentTitle];self.display.text = [self.display.text stringByAppendingString:digit]; }小技巧,可以按住option鍵,點擊某個方法或類,得到這個方法活類的文檔。
增加+ - * /符號的按鈕,增加Enter 按鈕,參數(shù)是None:
建立一個model
新建文件,CalculatorBrain。它是計算器的大腦
把操作壓入棧,完成棧上的操作。
@synthesize的實現(xiàn)getter setter的樣子。
上面operandStack的實例化是延遲實例化 ,這個方式在iOS里經(jīng)常用的。
這里是brain的.m文件和.h文件代碼:
// // CalculatorBrain.m // Calculator // // Created by rongfzh on 12-11-22. // Copyright (c) 2012年 rongfzh. All rights reserved. //#import "CalculatorBrain.h"@interface CalculatorBrain() @property (nonatomic, strong) NSMutableArray *operandStack; @end@implementation CalculatorBrain @synthesize operandStack = _operandStack;- (NSMutableArray *)operandStack {if (_operandStack == nil) {_operandStack = [[NSMutableArray alloc] init];}return _operandStack; } - (void)setOperandStack:(NSMutableArray *)operandStack {_operandStack = operandStack; }- (void)pushOperand:(double)operand{[self.operandStack addObject:[NSNumber numberWithDouble:operand]];}-(double)popOperand {NSNumber *operandObject = [self.operandStack lastObject];if (operandObject != nil) {[self.operandStack removeLastObject];}return [operandObject doubleValue]; } - (double)performOperation:(NSString *)operation{double result = 0;if ([operation isEqualToString:@"+"]) {result = [self popOperand] + [self popOperand];}[self pushOperand:result];return result; } @end#import <Foundation/Foundation.h>@interface CalculatorBrain : NSObject - (void)pushOperand:(double)operand; - (double)performOperation:(NSString *)operation; @end里面的具體的解釋看老頭講解吧。主要的流程是把操作數(shù)放到棧里,按下操作符時取出操作數(shù)進行相應(yīng)的運算。這節(jié)課實現(xiàn)了+法。
controller的代碼實現(xiàn):
#import "CalculatorViewController.h" #import "CalculatorBrain.h"@interface CalculatorViewController () @property (nonatomic) BOOL userIsInTherMiddleOfEnteringANumber; @property (nonatomic , strong) CalculatorBrain *brain; @end@implementation CalculatorViewController@synthesize brain = _brain;- (CalculatorBrain *)brain {if (!_brain) {_brain = [[CalculatorBrain alloc] init];}return _brain; } - (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }- (IBAction)digitPressed:(UIButton *)sender {NSString *digit = [sender currentTitle];if (self.userIsInTherMiddleOfEnteringANumber) {self.display.text = [self.display.text stringByAppendingString:digit];}else{self.display.text = digit;self.userIsInTherMiddleOfEnteringANumber = YES;} }- (IBAction)operationPressed:(UIButton*)sender {if (self.userIsInTherMiddleOfEnteringANumber) {[self enterPressed];}double result = [self.brain performOperation:sender.currentTitle];NSString *resultString = [NSString stringWithFormat:@"%g", result];self.display.text = resultString; }- (IBAction)enterPressed {[self.brain pushOperand:[self.display.text doubleValue]];self.userIsInTherMiddleOfEnteringANumber = NO; } @end運行結(jié)果:
課程代碼下載:http://download.csdn.net/detail/totogo2010/4798557
容芳志 (http://blog.csdn.net/totogo2010)
本文遵循“署名-非商業(yè)用途-保持一致”創(chuàng)作公用協(xié)議
轉(zhuǎn)載于:https://my.oschina.net/201003674/blog/288767
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的2011斯坦福大学iOS应用开发教程学习笔记(第二课)My First iOS App的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GPT 版超级马里奥来了,输入文本即可自
- 下一篇: mongodb服务部署