天天看點

UI控件筆記(十三):UI之MVC

一、MVC

M:模型(一個類,處理資料源)  V:視圖(View,進行排版)   C:控制器(VC,将資料源指派給相應排版的地方)

1、單一視圖

1.1、MainModel.h  ——  M

#import <Foundation/Foundation.h>

//模型

//模型就是為了做屬性而存在的,就是為了存資料用的

@interface MainModel : NSObject

//屬性名要和字典的key一樣,不然賦不上值

@property(nonatomic,retain)NSString *pic;

@property(nonatomic,retain)NSString *name;

@property(nonatomic,retain)NSString *price;

@property(nonatomic,retain)NSString *author;

@property(nonatomic,retain)NSString *country;

@property(nonatomic,retain)NSString *info;

@property(nonatomic,retain)NSString *type;

-(id)initWithDic:(NSDictionary*)dic;//初始化方法

-(void)setValue:(id)value forUndefinedKey:(NSString *)key;//防崩

@end

1.2、MainModel.m  ——  M

#import "MainModel.h"

@implementation MainModel

-(void)dealloc

{

    self.name = nil;

    self.price = nil;

    self.info = nil;

    self.author = nil;

    self.country = nil;

    self.pic = nil;

    self.type = nil;

    [super dealloc];

}

-(id)initWithDic:(NSDictionary *)dic

{

    self = [super init];

    if(self)

    {

        [self setValuesForKeysWithDictionary:dic];//kvc給這個類的屬性指派

    }

    return self;

}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key

{//實作就行,防崩

    NSLog(@"%@",key);

}

@end

1.3、MainView.h  ——  V

#import <UIKit/UIKit.h>

//視圖,就是用來做UI的

//視圖裡應該做布局,然後有一些屬性,可以用來給布局上的東西指派(圖、字、對象、方法)

@interface MainView : UIView

@property(nonatomic,retain)UIImageView *pic;

@property(nonatomic,retain)UILabel *nameLab;

@property(nonatomic,retain)UILabel *pri;

@property(nonatomic,retain)UILabel *auth;

@property(nonatomic,retain)UILabel *info;

@property(nonatomic,retain)UILabel *type;

@property(nonatomic,retain)UILabel *country;

@property(nonatomic,retain)UIButton *btn;

@end

1.4、MainView.m  ——  V

#import "MainView.h"

@implementation MainView

-(void)dealloc

{

    self.nameLab = nil;

    self.pic = nil;

    self.pri = nil;

    self.auth = nil;

    self.country = nil;

    self.info = nil;

    self.type = nil;

    self.btn = nil;

    [super dealloc];

}

-(id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if(self)

    {

        [self makeUI];

    }

    return self;

}

-(void)makeUI

{

    self.pic = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 80, 120)];

    [self addSubview:self.pic];

    [self.pic release];

    self.nameLab = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 210, 30)];

    self.nameLab.font = [UIFont boldSystemFontOfSize:18];

    [self addSubview:self.nameLab];

    [self.nameLab release];

    self.country = [[UILabel alloc] initWithFrame:CGRectMake(100, 40, 210, 20)];

    [self addSubview:self.country];

    [self.country release];

    self.pri = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 210, 20)];

    [self addSubview:self.pri];

    [self.pri release];

    self.auth = [[UILabel alloc] initWithFrame:CGRectMake(100, 80, 210, 20)];

    [self addSubview:self.auth];

    [self.auth release];

    self.type = [[UILabel alloc] initWithFrame:CGRectMake(100, 100,210, 20)];

    [self addSubview:self.type];

    [self.type release];

    self.info = [[UILabel alloc] initWithFrame:CGRectMake(10, 140, 300, 30)];

    [self addSubview:self.info];

    [self.info release];

    self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.btn.frame = CGRectMake(10, 170, 300, 30);

    self.btn.backgroundColor = [UIColor greenColor];

    [self.btn setTitle:@"觀看" forState:UIControlStateNormal];

    [self addSubview:self.btn];

}

@end

1.5、MainViewController.h  ——  C

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

1.6、MainViewController.m  ——  C

#import "ViewController.h"

#import "MainModel.h"

#import "MainView.h"

@interface ViewController ()

@property(nonatomic,retain)MainModel *model;

@property(nonatomic,retain)NSMutableArray *dataArr;

@end

@implementation ViewController

-(void)dealloc

{

    self.dataArr = nil;

    self.model = nil;

    [super dealloc];

}

- (void)viewDidLoad {

    [super viewDidLoad];   

    self.dataArr = [NSMutableArray arrayWithCapacity:0];    

    [self loadData];

    [self makeUI];

}

-(void)loadData

{

    NSArray *picArr = @[@"火影01.png",@"火影02.png"];

    NSArray *nameArr = @[@"火影忍者第一集",@"火影忍者OVA"];

    NSArray *infoArr = @[@"一堆pp打架",@"一堆qq打架"];

    NSArray *priceArr = @[@"1",@"2"];

    NSArray *typeArr = @[@"comic",@"movie"];

    NSArray *countryArr = @[@"日本",@"日本"];

    NSArray *authorArr = @[@"XX",@"XX"];

    for(int i = 0;i<picArr.count;i++)

    {

        NSDictionary *dic = @{@"pic":picArr[i],@"name":nameArr[i],@"info":infoArr[i],@"price":priceArr[i],@"type":typeArr[i],@"country":countryArr[i],@"author":authorArr[i]};

        //把字典的資料轉為存進模型對象中

        MainModel *model = [[MainModel alloc] initWithDic:dic];

        [self.dataArr addObject:model];//每次循環做一個model,然後把model存進數組

        [model release];

        //數組裡存着model

    }

}

-(void)makeUI

{//隻要把做好的View貼過來就行了

    for(int i = 0;i<self.dataArr.count;i++)

    {

        MainView *mainView = [[MainView alloc] initWithFrame:CGRectMake(0, 64+i*220, 320, 220)];

        [self.view addSubview:mainView];

        [mainView release];

        //上面這樣,布局就完事了

        MainModel *model = self.dataArr[i];        

        //下面用模型給V的屬性UI指派

        mainView.pic.image = [UIImage imageNamed:model.pic];

        mainView.nameLab.text = model.name;

        mainView.pri.text = model.price;

        mainView.info.text = model.info;

        mainView.country.text = model.country;

        mainView.auth.text = model.author;

        mainView.type.text = model.type;

        [mainView.btn addTarget:self action:@selector(btnDown) forControlEvents:UIControlEventTouchUpInside];

    }

}

-(void)btnDown

{

    NSLog(@"開始看火影");

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

@end

2、Table使用MVC

2.1、MainModel.h  ——  M

#import <Foundation/Foundation.h>

@interface MainModel : NSObject

@property(nonatomic,retain)NSString *name;

@property(nonatomic,retain)NSString *age;

@property(nonatomic,retain)NSString *sex;

@property(nonatomic,retain)NSString *height;

@property(nonatomic,retain)NSString *weight;

-(id)initWithDic:(NSDictionary*)dic;

-(void)setValue:(id)value forUndefinedKey:(NSString *)key;

@end

2.2、MainModel.m  ——  M

#import "MainModel.h"

@implementation MainModel

-(void)dealloc

{

    self.name = nil;

    self.sex = nil;

    self.age = nil;

    self.height = nil;

    self.weight = nil;

    [super dealloc];

}

-(id)initWithDic:(NSDictionary *)dic

{

    self = [super init];

    if(self)

    {

        [self setValuesForKeysWithDictionary:dic];

    }

    return self;

}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key

{

    NSLog(@"%@",key);

}

@end

2.3、MainTableViewCell.h  ——  V

#import <UIKit/UIKit.h>

@interface MainTableViewCell : UITableViewCell

//布局

@property(nonatomic,retain)UILabel *nameLabel;

@property(nonatomic,retain)UILabel *ageLabel;

@property(nonatomic,retain)UILabel *sexLabel;

@property(nonatomic,retain)UILabel *heightLabel;

@property(nonatomic,retain)UILabel *weightLabel;

@property(nonatomic,retain)UIButton *delBtn;

@property(nonatomic,retain)UIImageView *imageVie;

@end

2.4、MainTableViewCell.m  ——  V

#import "MainTableViewCell.h"

@implementation MainTableViewCell

-(void)dealloc

{

    self.nameLabel = nil;

    self.ageLabel = nil;

    self.sexLabel = nil;

    self.heightLabel = nil;

    self.weightLabel = nil;

    self.imageVie = nil;

    self.delBtn = nil;

    [super dealloc];

}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if(self)

    {

        //布局

        [self makeUI];

    }

    return self;

}

-(void)makeUI

{

    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 250, 40)];

    self.nameLabel.font = [UIFont boldSystemFontOfSize:17];

    [self.contentView addSubview:self.nameLabel];

    [self.nameLabel release];

    self.ageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 45, 250, 40)];

    [self.contentView addSubview:self.ageLabel];

    [self.ageLabel release];

    self.sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 85, 250, 40)];

    [self.contentView addSubview:self.sexLabel];

    [self.sexLabel release];

    self.heightLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 125, 250, 40)];

    [self.contentView addSubview:self.heightLabel];

    [self.heightLabel release];

    self.weightLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 165, 250, 40)];

    [self.contentView addSubview:self.weightLabel];

    [self.weightLabel release];

    self.imageVie = [[UIImageView alloc] initWithFrame:CGRectMake(280, 0, 40, 40)];

    [self.contentView addSubview:self.imageVie];

    [self.imageVie release];

    self.delBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.delBtn.frame = CGRectMake(270, 5, 40, 200);

    self.delBtn.backgroundColor = [UIColor yellowColor];

    [self.delBtn setTitle:@"del" forState:UIControlStateNormal];

    [self.contentView addSubview:self.delBtn];

}

- (void)awakeFromNib {

    // Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state

}

@end

2.5、MainViewController.h  ——  C

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController

@end

2.6、MainViewController.m  ——  C

#import "MainViewController.h"

#import "MainModel.h"

#import "MainTableViewCell.h"

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *dataArr;

@end

@implementation MainViewController

-(void)dealloc

{

    self.dataArr = nil;

    [super dealloc];

}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if(self)

    {

        self.dataArr = [NSMutableArray arrayWithCapacity:0];

    }

    return self;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;

    [self loadData];

    [self makeUI];

    // Do any additional setup after loading the view.

}

-(void)loadData

{

    NSArray *nameArr = @[@"lily",@"lucy",@"pp",@"qq",@"tt",@"oo"];

    for(int i = 0;i<nameArr.count;i++)

    {

        NSDictionary *dic = @{@"name":nameArr[i],@"age":[NSString stringWithFormat:@"%d歲",arc4random()%100],@"sex":@"female",@"height":[NSString stringWithFormat:@"%dcm",arc4random()%200],@"weight":[NSString stringWithFormat:@"%dkg",arc4random()%100]};

        //執行個體化模型,并用字典初始化

        MainModel *model = [[MainModel alloc] initWithDic:dic];

        //模型存數組

        [self.dataArr addObject:model];

        //模型對象-1

        [model release];

    }

    NSLog(@"%@",self.dataArr);

}

-(void)makeUI

{

    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, self.view.frame.size.height-64) style:UITableViewStylePlain];

    table.dataSource = self;

    table.delegate = self;

    table.tag = 6666;

    [self.view addSubview:table];

    [table release];

}

#pragma mark table代理

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.dataArr.count;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *iden = @"pp";

    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];//自定義cell

    if(cell==nil)

    {

        cell = [[[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];

    }

    //指派給自定義cell的UI控件

    MainModel *model = self.dataArr[indexPath.row];//從資料源裡找到目前行所要用的模型   

    cell.nameLabel.text = model.name;

    cell.ageLabel.text = model.age;

    cell.sexLabel.text = model.sex;

    cell.heightLabel.text = model.height;

    cell.weightLabel.text = model.weight;

    cell.imageVie.image = [UIImage imageNamed:@"火影08.png"];

    //自定義cell時,cell的屬性不能叫imageView

    [cell.delBtn addTarget:self action:@selector(btnDown:) forControlEvents:UIControlEventTouchUpInside];

    cell.delBtn.tag = 1000+indexPath.row;

    return cell;

}

-(void)btnDown:(UIButton*)btn

{//點誰删誰

    //删資料源對應行的資料

    [self.dataArr removeObjectAtIndex:btn.tag - 1000];

    //重新整理table

    UITableView *table = (UITableView*)[self.view viewWithTag:6666];

    [table reloadData];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 210;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

@end

二、MVC中的V的情況

1、一個V裡有一些UI,沒有類似的

2、循環V,多個類似的V ——  一個簡單的,tableView

3、一個V裡有一些類似的UI —— 詳情頁面

2.1、UpModel.h  —— M

#import <Foundation/Foundation.h>

@interface UpModel : NSObject

@property(nonatomic,retain)NSArray *picNameArr;//存圖檔

@property(nonatomic,retain)NSArray *labelNameArr;//存圖檔下面字的

-(id)initWithDic:(NSDictionary*)dic;

-(void)setValue:(id)value forUndefinedKey:(NSString *)key;

@end

2.2、UpModel.m  —— M

#import "UpModel.h"

@implementation UpModel

-(void)dealloc

{

    self.picNameArr = nil;

    self.labelNameArr = nil;

    [super dealloc];

}

-(id)initWithDic:(NSDictionary *)dic

{

    self = [super init];

    if(self)

    {

        [self setValuesForKeysWithDictionary:dic];

    }

    return self;

}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key

{

    NSLog(@"%@",key);

}

@end

2.3、UpView.h —— V

#import <UIKit/UIKit.h>

@interface UpView : UIView

@end

2.4、UpView.m —— V

#import "UpView.h"

@implementation UpView

-(id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if(self)

    {

        [self makeUI];

    }

    return self;

}

-(void)makeUI

{

    for(int i = 0;i<3;i++)

    {

        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10+i*100, 10, 100, 60)];

        [self addSubview:img];

        [img release];

        img.tag = 3000+i;

        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(10+i*100, 170, 100, 30)];

        [self addSubview:lab];

        [lab release];

        lab.tag = 2000+i;

    }

}

@end

2.5、MainViewController.h  ——  C

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController

@end

2.6、MainViewController.m  ——  C

#import "MainViewController.h"

#import "UpModel.h"

#import "UpView.h"

@interface MainViewController ()

@property(nonatomic,retain)UpModel *upModel;

@end

@implementation MainViewController

-(void)dealloc

{

    self.upModel = nil;

    [super dealloc];

}

- (void)viewDidLoad {

    [super viewDidLoad]; 

    [self loadData];  

    [self makeUI];

}

-(void)loadData

{

    [self loadUpData];

    [self loadUnderData];

}

-(void)loadUpData

{

    NSDictionary *dic = @{@"picNameArr":@[@"火影01.png",@"火影02.png",@"火影03.png"],@"labelNameArr":@[@"火影01",@"火影02",@"火影03"]};

    //字典

    //模型

    self.upModel = [[UpModel alloc] initWithDic:dic];

    [self.upModel release];

}

-(void)loadUnderData

{

}

-(void)makeUI

{

    [self makeUpUI];

    [self makeUnderUI];

}

-(void)makeUpUI

{

    UpView *upView = [[UpView alloc] initWithFrame:CGRectMake(0, 64, 320, 210)];

    [self.view addSubview:upView];

    [upView release];

    for(int i = 0;i<3;i++)

    {

        UIImageView *img = (UIImageView*)[self.view viewWithTag:3000+i];

        UILabel *lab = (UILabel*)[self.view viewWithTag:2000+i];

        img.image = [UIImage imageNamed:self.upModel.picNameArr[i]];

        lab.text = self.upModel.labelNameArr[i];

    }

}

-(void)makeUnderUI

{

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end