ios-day03-03(xib和自定义View的结合使用,MVC模式)

效果图:


假设每个应用对应一个UIView,UIView中又有3个控件(一个UIImageView,一个UILabel,一个UIButton),在前面的文章中我们使用纯代码的方式来创建这些UIView。这里我们使用自定义UIView的方式,结合MVC设计模式来实现上图所示的界面效果。


MVC——Model、View、Controller

Model:模型——存储数据

View:视图(界面)——显示

Controller:控制器——获取模型中存储的数据,然后提供给View进行显示


思路:

1,加载.plist文件中的数据(.plist文件中存储的是应用名、应用图标名)

2,解析.plist文件中的数据,将每个应用对应的数据都封装到一个模型中

3,自定义UIView,该UIView根据模型进行初始化(初始化方法中接受一个模型对象,根据模型对象中的数据对UIView中的子控件进行设置)

以上3步都是通过控制器来控制执行


源代码下载地址:http://download.csdn.net/detail/liu537192/8426005


下面附上核心代码:

//
//  LiuJieApp.h
//  02-应用管理
//
//  Created by XinYou on 15-2-5.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
    
    
     
     

/**
 copy : NSString
 strong: 一般对象
 weak: UI控件
 assign:基本数据类型
 */

@interface LiuJieApp : UIView

/**
 *  图标
 */
@property (nonatomic, copy) NSString *icon;
/**
 *  名称
 */
@property (nonatomic, copy) NSString *name;

// 提供两个初始化方法,一个动态,一个静态
- (instancetype)initAppWithDict:(NSDictionary *)dict;

/**
 *  通过字典对象来初始化模型对象
 *
 *  @param dict 字典对象
 *
 *  @return 模型对象
 */
+ (instancetype)appWithDict:(NSDictionary *)dict;

@end
//
//  LiuJieApp.m
//  02-应用管理
//
//  Created by XinYou on 15-2-5.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieApp.h"

@implementation LiuJieApp

- (instancetype)initAppWithDict:(NSDictionary *)dict{
    
    if (self = [super init]) {
        self.icon = dict[@"icon"];
        self.name = dict[@"name"];
    }
    
    return self;
}

+ (instancetype)appWithDict:(NSDictionary *)dict{
    
    // 这里为什么能用self呢?在静态方法中,self表示当前类名
    // 相当于[LiuJieApp alloc]
    return [[self alloc] initAppWithDict:dict];

}

@end
//
//  LiuJieAppView.h
//  02-应用管理
//
//  Created by XinYou on 15-2-5.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
     
     
      
      
@class LiuJieApp;

@interface LiuJieAppView : UIView
/**
 *  模型数据
 */
@property (nonatomic, strong) LiuJieApp *app;

+ (instancetype)appView;

/**
 *  通过模型数据来创建一个View
 *
 */
+ (instancetype)appViewWithApp:(LiuJieApp *)app;

@end
//
//  LiuJieAppView.m
//  02-应用管理
//
//  Created by XinYou on 15-2-5.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieAppView.h"
#import "LiuJieApp.h"

@interface LiuJieAppView()

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@end

@implementation LiuJieAppView


+ (instancetype)appView{
    
    NSBundle *bundle = [NSBundle mainBundle];
    // 读取xib文件(会创建xib中的描述的所有对象,并且按顺序放到数组中返回)
    NSArray *objs = [bundle loadNibNamed:@"LJAppView" owner:nil options:nil];
    
    LiuJieAppView *appView = [objs lastObject];
    
    return appView;
}

+ (instancetype)appViewWithApp:(LiuJieApp *)app{
    
    LiuJieAppView *appView = [self appView];

//    appView.iconView.image = [UIImage imageNamed:app.icon];
//    
//    appView.nameLabel.text = app.name;

    appView.app = app;
    
    return appView;
}

- (void)setApp:(LiuJieApp *)app{
    
    _app = app;
    // 1.设置图标
    self.iconView.image =[UIImage imageNamed:app.icon];
    // 2.设置名称
    self.nameLabel.text = app.name;
    
}

@end
//
//  LiuJieViewController.m
//  02-应用管理
//
//  Created by XinYou on 15-2-5.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieViewController.h"
#import "LiuJieAppView.h"
#import "LiuJieApp.h"
@interface LiuJieViewController ()
/**
 *  存放应用信息
 */
@property (nonatomic, strong) NSArray *apps;

@end

@implementation LiuJieViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 0.总列数(一行最多3列)
    int totalColumns = 3;
    
    // 1.应用的尺寸
    CGFloat appW = 85;
    CGFloat appH = 90;
    
    // 2.间隙 = (控制器view的宽度 - totalColumns * 应用宽度) / (totalColumns + 1)
    CGFloat marginX = (self.view.frame.size.width - totalColumns * appW) / (totalColumns + 1);
    CGFloat marginY = 15;
    
    for (int index = 0; index < self.apps.count; index++) {
        // 3.1.创建view
        LiuJieAppView *appView = [LiuJieAppView appViewWithApp:self.apps[index]];
        // 3.2.添加view
        [self.view addSubview:appView];
        
        // 3.3.设置frame
        int row = index / totalColumns;
        int col = index % totalColumns;
        // 计算x和y
        CGFloat appX = marginX + col * (appW + marginX);
        CGFloat appY = 30 + row * (appH + marginY);
        appView.frame = CGRectMake(appX, appY, appW, appH);
    }
    
    
}

- (NSArray *)apps{

    if(_apps == nil){
    
        // 初始化apps
        
        // 1,拿到app.plist文件的路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
        // 2,加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        // 3,将dictArray里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *appArray = [NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            // 3.1,创建模型对象
            LiuJieApp *app = [LiuJieApp appWithDict:dict];
            // 3.2,添加模型对象到数组中
            [appArray addObject:app];
        }
        // 4,赋值
        _apps = appArray;
    }
    
    
    return _apps;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

     
     
    
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值