UI控件笔记(十三):UI之MVC

一、MVC

M:模型(一个类,处理数据源)  V:视图(View,进行排版)   C:控制器(VC,将数据源赋值给相应排版的地方)


1、单一视图


1.1、MainModel.h  ——  M


#import

//模型

//模型就是为了做属性而存在的,就是为了存数据用的

@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

//视图,就是用来做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

@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;icount;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

@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


@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

@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;icount;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];

    }

    //赋值给自定义cellUI控件

    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

@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

@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

@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











你可能感兴趣的:(IOS入门之UI控件,控件,ui,mvc,iOS)