第二课名称是: My First iOS App 我的第一个iOS应用
注意:我用的是XCodeVersion 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 (weak, nonatomic) IBOutlet UILabel *display;
IBOutlet 这个类型没有具体的内容,只是Xcode用来跟踪那个 property是Outlet。 编译器会忽略它,没有什么实际内容。
添加一些按钮。
Round Rect Button。
这就使用到了MVC的target -action,
在.m文件里生成了代码如下:
- (IBAction)digitPressed:(id)sender {
}
其实 IBAction什么都不是,只是让Xcode知道这是个aciton,事实上IBAction是个void。
消息的参数 sender ,就是按钮自己,id是啥呢? 是个很总要的类型,可以指向任何类型对象的指针。
复制多个按钮,复制好之后改变按钮上的数字。
当你复制按钮的时候,也复制了它的target- action
修改代码:
- (IBAction)digitPressed:(UIButton *)sender {
NSString *digit = [sender currentTitle];
NSLog(@"digit pressed = %@", digit);
}
取得当前按下按钮的title,并在控制台打印出来。
运行程序看看,第一次运行比较慢,因为要预编译一些文件,读入framework等,第二次就快了。
在Label 上显示数字,添加代码如下:
- (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 按钮,参数是None:
建立一个model
新建文件,CalculatorBrain。它是计算器的大脑
把操作压入栈,完成栈上的操作。
@synthesize的实现getter setter的样子。
- (NSMutableArray *)operandStack
{
if (_operandStack == nil) {
_operandStack = [[NSMutableArray alloc] init];
}
return _operandStack;
}
- (void)setOperandStack:(NSMutableArray *)operandStack
{
_operandStack = operandStack;
}
上面operandStack的实例化是延迟实例化 ,这个方式在iOS里经常用的。
这里是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
里面的具体的解释看老头讲解吧。主要的流程是把操作数放到栈里,按下操作符时取出操作数进行相应的运算。这节课实现了+法。
controller的代码实现:
#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
运行结果: