码迷,mamicode.com
首页 > 移动开发 > 详细

developing apps for IOS---Stanford CS193p---Introduction and MVC--1

时间:2015-03-13 16:03:05      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

其实要学习的应该是os x开发,但是资料较少,干脆先把iOS学了,大体应该是一样的。

学习源为stanford的公开课 非常推荐 白胡子帅老头 简直太棒了 赏心悦目 讲解也是深入浅出 

http://v.163.com/special/opencourse/ios7.html

so,let‘s start!

====================================================

what‘s in IOS?

IOS has four layers and from bottom to top is :

Core OS: os x kener unixos c language

Core Services:network thread and location and so on

Media: you know!

Cocoa Touch: we always work on this layer. fully object-oriented

Platform Components

tools: Xcode and instruments 

language:objective-c

frameworks:Foundation

Design Strategies:MVC

MVC

 

 

 技术分享

 the slide is from the lecture the Dr. give .

Model:

what your app is.

Controller:

how your model is presented to the user(UI logic)

View:

your controller‘s minions(仆人)。

 

outlet is a term that we use descripe a property in the controller that uses to talk to its view.

view talk to controller: controller set the target,hand it to view,and view come out an event,return the action to controller.

view tell the controller about when didi should and controller design delegation to realize.

controller interpret and format model information for the view.

controller‘s job:把model的信息传给view响应delegation消息。

Notification&KVO is radio station to make the sync with model and controller.

 

Objective-c


//
//  Card.h
//  Machismo
//
//  Created by 一棋王 on 15-3-12.
//  Copyright (c) 2015年 melody5417. All rights reserved.
//

//.h is your public API .m includes @interface (is private) and implementation
#import <Foundation/Foundation.h>
//ios7 change to  @import Foundation but always support the old


@interface Card : NSObject
//superclass,NSObject is the root class of every class

@property (strong,nonatomic) NSString *contents;//what‘s on the card.
//in oc all objects live in the heap, and we have pointers to them
//oc will manage all that memory for you. create and free.
//in oc you can send messages to nil pointer,
//and if it has return it will return nil
//nonatomic is not thread safe
//strong and weak.every pointer must be either strong or weak.
//property will implemented setter and getter for us.

@property (nonatomic,getter=isChosen) BOOL chosen;//rename
@property (nonatomic,getter=isMatched) BOOL matched;//rename


//- (int)match:(Card *)card;//don‘t match=0 just depends on subclass playingCard
- (int)match:(NSArray*)onterCards;//dont miss the star*





@end

 


//
// Card.m // Machismo // // Created by 一棋王 on 15-3-12. // Copyright (c) 2015年 melody5417. All rights reserved. // //.m is your private API and all your implementation #import "Card.h" @interface Card()//private most for properties @end @implementation Card//do not need to specify superclass @synthesize contents = _contents; //_contents can be any name,it stands for the memory.name this just to see clearly. //synthesize can implemented setter and getter for you. //-(NSString *)contents //{ // return _contents; //} //- (void)setContents:(NSString *)contents //{ // _contents = contents; //} @synthesize chosen = _chosen; @synthesize matched = _matched; /* - (int)match:(Card *)card { int score = 0; //调用getter if([card.contents isEqualToString:self.contents]){ score = 1; } //we don‘t use == bacase ==is pointer not the contents the point to return score; } */ -(int) match:(NSArray*)onterCards { int score = 0; for (Card *card in onterCards) { if ([card.contents isEqualToString:self.contents]) { score = 1; } //most people use . dot notation its mor elegent and simplic //but notic use . dot notations only for getter and setter } return score; } @end


//
//  Deck.h
//  Machismo
//
//  Created by 一棋王 on 15-3-12.
//  Copyright (c) 2015年 melody5417. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Card.h"


@interface Deck : NSObject

- (void)addCard:(Card *)card atTop:(BOOL)atTop;
- (void)addCard:(Card *)card;
- (Card *)drawRandomCard;
@end

//
//  Deck.m
//  Machismo
//
//  Created by 一棋王 on 15-3-12.
//  Copyright (c) 2015年 melody5417. All rights reserved.
//
//card and deck is genetic not only for 扑克牌

#import "Deck.h"

@interface Deck ()
@property (strong,nonatomic) NSMutableArray *cards; //of card
@end

@implementation Deck

- (NSMutableArray *) cards
{
    //lazy instancilize
    if (!_cards) {
        _cards = [[NSMutableArray alloc]init];
    }
    return _cards;
}

- (void)addCard:(Card *)card atTop:(BOOL)atTop
{
    if(atTop){
        [self.cards insertObject:card atIndex:0];
    }else{
        [self.cards addObject:card];
    }
}

- (void)addCard:(Card *)card
{
    [self addCard:card atTop:NO];
}

- (Card *) drawRandomCard
{
    Card *randomCard = nil;
    
    if ([self.cards count]) {
        //remove if the mutableNumber is 0 then will ...
        unsigned index = arc4random() % [self.cards count];
        randomCard = self.cards[index];
        [self.cards removeObjectAtIndex:index];
    }
    
    return randomCard;
}




@end

 


//
//  PlayingCard.h
//  Machismo
//
//  Created by 一棋王 on 15-3-12.
//  Copyright (c) 2015年 melody5417. All rights reserved.
//

//card is genetic but playingcard is specify 扑克牌
#import "Card.h"

@interface PlayingCard : Card

@property (strong,nonatomic)NSString *suit;
@property (nonatomic) NSUInteger rank;

+ (NSArray *)validSuits;
+ (NSUInteger)maxRank;




@end

//
//  PlayingCard.m
//  Machismo
//
//  Created by 一棋王 on 15-3-12.
//  Copyright (c) 2015年 melody5417. All rights reserved.
//

#import "PlayingCard.h"

@implementation PlayingCard

@synthesize suit = _suit;

- (NSString *)contents{
    NSArray *rankStrings = [PlayingCard rankStrings];
    //the blue @ is call the array alloc and init
    return [rankStrings[self.rank] stringByAppendingString:self.suit];
}

+ (NSArray *)validSuits{
    return @[@"♣?",@"♥?",@"♦?",@"♠?"];
}

- (void) setSuit:(NSString *)suit{
    if([[PlayingCard validSuits] containsObject:suit])
    {
        _suit = suit;
    }
}

+ (NSArray *)rankStrings{
    return @[@"?",@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K"];
}

+ (NSUInteger) maxRank{
    return [[self rankStrings]count]-1;
}

- (void) setRank:(NSUInteger)rank{
    if (rank<=[PlayingCard maxRank]) {
        _rank = rank;
    }
}



@end
//
// PlayingCardDeck.h
// Machismo
//
// Created by 一棋王 on 15-3-12.
// Copyright (c) 2015年 melody5417. All rights reserved.
//

#import "Deck.h"

@interface PlayingCardDeck : Deck

@end

 

 

 


//
//  PlayingCardDeck.m
//  Machismo
//
//  Created by 一棋王 on 15-3-12.
//  Copyright (c) 2015年 melody5417. All rights reserved.
//  dot notaion is only for property setter and getter.
 #import "PlayingCardDeck.h" 
#import "PlayingCard.h"
@implementation PlayingCardDeck //alloc and init always be company never call one of them only and they call only once
//因为不是总返回一个类型 可能是superclass 返回一个对象 具有相同类类型 just use here
- (instancetype) init{ self = [super init]; if (self) {
//遍历所有suit and rank and addobject
for (NSString* suit in [PlayingCard validSuits]) {
for (NSUInteger rank = 1;
rank<=[PlayingCard maxRank];
rank++) {
PlayingCard
*card = [[PlayingCard alloc]init];
card.rank
= rank;
card.suit
= suit;
[self addCard:card];
} } }
return self; } @end

 


//
//  ViewController.h
//  Machismo
//
//  Created by 一棋王 on 15-3-12.
//  Copyright (c) 2015年 melody5417. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Card.h"
#import "Deck.h"
#import "PlayingCard.h"
#import "PlayingCardDeck.h"


@interface ViewController : UIViewController


@end

//
//  ViewController.m
//  Machismo
//
//  Created by 一棋王 on 15-3-12.
//  Copyright (c) 2015年 melody5417. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()//private
@property (weak, nonatomic) IBOutlet UILabel *flipsLable;
@property (nonatomic) int flipCount;

@property (strong,nonatomic) Deck *deck;
@end

@implementation ViewController

- (void)setFlipCount:(int)flipCount
{
    _flipCount = flipCount;
    self.flipsLable.text = [NSString stringWithFormat:@"flips: %d",self.flipCount];
    //getter and setter another function to keep UI sync with controller
    NSLog(@"flipCount = %d",self.flipCount);
}

//IBAction is typedef void, just to tell which method are target action.

- (IBAction)touchCardButton:(UIButton *)sender
{
    if ([sender.currentTitle length]) {
        [sender setBackgroundImage:[UIImage imageNamed:@"CardBack"] forState:UIControlStateNormal];
        [sender setTitle:@"" forState:UIControlStateNormal];
        self.flipCount++;
    }else{
        Card *card = [self.deck drawRandomCard];
        if(card)
        {
            [sender setBackgroundImage:[UIImage imageNamed:@"CardFront"]
                          forState:UIControlStateNormal];
            [sender setTitle:@"" forState:UIControlStateNormal];                
[sender setTitle:card.contents forState:UIControlStateNormal]; self.flipCount
++; } } //self.flipCount++;// 调用getter and setter } @synthesize deck = _deck; - (Deck *)deck { //lazy instantiation if (!_deck) { _deck = [self createDeck]; } return _deck; } - (Deck *)createDeck { return [[PlayingCardDeck alloc]init]; } @end

 

 

 

developing apps for IOS---Stanford CS193p---Introduction and MVC--1

标签:

原文地址:http://www.cnblogs.com/melody5417-bky/p/4335243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!