2011斯坦福大学iOS应用开发教程学习笔记(第二课)My First iOS App
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
第二課名稱是: My First iOS App 我的第一個(gè)iOS應(yīng)用?
注意:我用的是XCode?Version 4.5.2 (4G2008a)版本,SDK 是6.0,和視頻教程稍微不一樣。
這課主要是以一個(gè)計(jì)算器一個(gè)用為例子,教你怎么使用XCode,如何使用MVC設(shè)計(jì)模式創(chuàng)建應(yīng)用。
我們跟著他把應(yīng)用做出來(lái),這顆學(xué)習(xí)的目的就達(dá)到了。
1、新建一個(gè)single view application模版的應(yīng)用
填寫(xiě)項(xiàng)目信息。
前綴加上 Calculator,新建的Viewcontroller前面都帶有Calculator。選擇了 使用故事版,使用ARC,這都是iOS 5.0之后的新特性。單元測(cè)試就不選了,后期才會(huì)去學(xué)。
接下來(lái)按他的演示,在故事版的View上放下一個(gè)Label控件,并調(diào)整位置和大小,如圖:
創(chuàng)建outlet
好吧,老頭繼續(xù)把MVC模式的圖放了出來(lái):
為了說(shuō)明Controller創(chuàng)建一個(gè)outlet到view, 如何操作呢?在Label按住上按住Control鍵,拖到.h文件,放開(kāi)
要用weak指針,因?yàn)樗呀?jīng)在這個(gè)窗口上,有一個(gè)strong指針指向它了。所以只需要一個(gè)weak指針就行了。
@property (weak, nonatomic) IBOutlet UILabel *display;
IBOutlet 這個(gè)類型沒(méi)有具體的內(nèi)容,只是Xcode用來(lái)跟蹤那個(gè) property是Outlet。 編譯器會(huì)忽略它,沒(méi)有什么實(shí)際內(nèi)容。
添加一些按鈕。
Round Rect Button。
這就使用到了MVC的target -action,
在.m文件里生成了代碼如下:
- (IBAction)digitPressed:(id)sender { }其實(shí) IBAction什么都不是,只是讓Xcode知道這是個(gè)aciton,事實(shí)上?IBAction是個(gè)void。消息的參數(shù) sender ,就是按鈕自己,id是啥呢? 是個(gè)很總要的類型,可以指向任何類型對(duì)象的指針。
復(fù)制多個(gè)按鈕,復(fù)制好之后改變按鈕上的數(shù)字。
當(dāng)你復(fù)制按鈕的時(shí)候,也復(fù)制了它的target- action
修改代碼:
- (IBAction)digitPressed:(UIButton *)sender {NSString *digit = [sender currentTitle];NSLog(@"digit pressed = %@", digit); }取得當(dāng)前按下按鈕的title,并在控制臺(tái)打印出來(lái)。
運(yùn)行程序看看,第一次運(yùn)行比較慢,因?yàn)橐A(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]; }后面注釋的是另外一種寫(xiě)法,和.號(hào)的寫(xiě)法一個(gè)作用。上面的代碼可以縮減成這樣:
- (IBAction)digitPressed:(UIButton *)sender {NSString *digit = [sender currentTitle];self.display.text = [self.display.text stringByAppendingString:digit]; }小技巧,可以按住option鍵,點(diǎn)擊某個(gè)方法或類,得到這個(gè)方法活類的文檔。
增加+ - * /符號(hào)的按鈕,增加Enter 按鈕,參數(shù)是None:
建立一個(gè)model
新建文件,CalculatorBrain。它是計(jì)算器的大腦
把操作壓入棧,完成棧上的操作。
@synthesize的實(shí)現(xiàn)getter setter的樣子。
上面operandStack的實(shí)例化是延遲實(shí)例化 ,這個(gè)方式在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í)取出操作數(shù)進(jìn)行相應(yīng)的運(yùn)算。這節(jié)課實(shí)現(xiàn)了+法。
controller的代碼實(shí)現(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運(yùn)行結(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)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的2011斯坦福大学iOS应用开发教程学习笔记(第二课)My First iOS App的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: GPT 版超级马里奥来了,输入文本即可自
- 下一篇: abt格式怎么打开(嵌入式实习报告)