用mvc的应用管理代码

控制器:LWTViewController.m

 1 //
 2 //  LWTViewController.m
 3 //  应用管理--九宫格
 4 //
 5 //  Created by apple on 14-5-22.
 6 //  Copyright (c) 2014年 lwt. All rights reserved.
 7 //
 8 
 9 #import "LWTViewController.h"
10 #import "LWTAppInfoList.h"
11 #import "LWTAppInfoView.h"
12 
13 @interface LWTViewController ()
14 
15 // 保存plist文件内容
16 @property (nonatomic, strong) NSArray *appInfoList;
17 
18 @end
19 
20 @implementation LWTViewController
21 
22 #pragma mark - 优化模型 未完成
23 - (NSArray *)appInfoList
24 {
25     if (!_appInfoList) {
26         // 用模型获取plist文件内容
27         _appInfoList = [LWTAppInfoList appInfoList];
28     }
29     return _appInfoList;
30     
31 }
32 
33 - (void)viewDidLoad
34 {
35     [super viewDidLoad];
36     // Do any additional setup after loading the view, typically from a nib.
37     
38     // 每行3列
39     int totalcols = 3;
40     // 起始高度
41     CGFloat viewStart = 20;
42     // 高度间隔
43     CGFloat viewH = 10;
44     
45     for (int i = 0; i < self.appInfoList.count; i++) {
46         // 行数
47         int row = i / totalcols;
48         // 列数
49         int col = i % totalcols;
50         
51         // 该条目的title和图片名称
52         LWTAppInfoList *dict = self.appInfoList[i];
53         // 从视图获取小view信息
54         UIView *view = [LWTAppInfoView appInfoViewWithAppInfoList:dict];
55         
56         // 小view的宽高
57         CGFloat viewWidth = view.bounds.size.width;
58         CGFloat viewHeight = view.bounds.size.height;
59         
60         // 两个view的宽度间隔
61         CGFloat viewM = (self.view.bounds.size.width - (viewWidth * totalcols)) / (totalcols + 1);
62         
63         // 每个view的x
64         int viewX = viewM + ((viewWidth + viewM)* col);
65         // 每个view的y
66         int viewY = viewStart + viewH + (viewH + viewHeight) * row;
67         
68         
69         view.frame = CGRectMake(viewX, viewY, viewWidth, viewHeight);
70         [self.view addSubview:view];
71     }
72 }
73 
74 @end

模型 .h:LWTAppInfoList.h

 1 //
 2 //  LWTAppInfoList.h
 3 //  应用管理--九宫格
 4 //
 5 //  Created by apple on 14-5-22.
 6 //  Copyright (c) 2014年 lwt. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface LWTAppInfoList : NSObject
12 // plist属性
13 @property (nonatomic, copy) NSString *name;
14 @property (nonatomic, copy) NSString *icon;
15 // 创建图片,控制器直接调用图片
16 @property (nonatomic, weak, readonly) UIImage *image;
17 
18 // init重构
19 - (instancetype)initWithNSDictionary : (NSDictionary *)dict;
20 // 工厂方法
21 + (instancetype) appInfoListWithDictionary : (NSDictionary *)dict;
22 // 封装plist方法,使控制器直接调用就可获取plist内容
23 + (NSArray *)appInfoList;
24 
25 @end

模型.m:LWTAppInfoList.m

//
//  LWTAppInfoList.m
//  应用管理--九宫格
//
//  Created by apple on 14-5-22.
//  Copyright (c) 2014年 lwt. All rights reserved.
//

#import "LWTAppInfoList.h"

#pragma mark 定义私有属性
@interface LWTAppInfoList ()
{
    UIImage *_image;
}

@end

@implementation LWTAppInfoList

- (UIImage *)image
{
    _image = [UIImage imageNamed:_icon];
    return _image;
}

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

+ (instancetype)appInfoListWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithNSDictionary:dict];
}

+ (NSArray *)appInfoList
{
    // 根据Plist文件获取内容
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:@"app.plist" ofType:nil];
    // 获取plist内容
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    // 创建临时数组
    NSMutableArray *arrayM = [NSMutableArray array];
    for ( NSDictionary *dict in array)
    {
        // 创建LWTAppInfoList对象
        // 将对象赋给数组
        [arrayM addObject:[self appInfoListWithDictionary:dict]];
    }
    // 返回数组
    return arrayM;
}

@end

视图 .h:LWTAppInfoView.h

 1 //
 2 //  LWTAppInfoView.h
 3 //  应用管理--九宫格
 4 //
 5 //  Created by apple on 14-5-23.
 6 //  Copyright (c) 2014年 lwt. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 @class LWTAppInfoList;
11 
12 @interface LWTAppInfoView : UIView
13 
14 + (instancetype) appInfoViewWithAppInfoList : (LWTAppInfoList *)appInfo;
15 
16 
17 @end

视图 .m:LWTAppInfoView.m

 1 //
 2 //  LWTAppInfoView.m
 3 //  应用管理--九宫格
 4 //
 5 //  Created by apple on 14-5-23.
 6 //  Copyright (c) 2014年 lwt. All rights reserved.
 7 //
 8 
 9 #import "LWTAppInfoView.h"
10 #import "LWTAppInfoList.h"
11 
12 @interface LWTAppInfoView ()
13 
14 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
15 
16 @property (weak, nonatomic) IBOutlet UILabel *label;
17 
18 @property (nonatomic, strong) LWTAppInfoList *appInfo;
19 
20 @property (weak, nonatomic) IBOutlet UIButton *downloadButton;
21 
22 
23 @end
24 
25 @implementation LWTAppInfoView
26 
27 + (instancetype)appInfoView
28 {
29     // 读取xib文件中的view数组
30     NSArray *arrayViews = [[NSBundle mainBundle] loadNibNamed:@"appInfoView" owner:nil options:nil];
31     // xib中的第一个数组就是所需数组
32     return [arrayViews firstObject];
33 }
34 
35 
36 + (instancetype)appInfoViewWithAppInfoList:(LWTAppInfoList *)appInfo
37 {
38     LWTAppInfoView *view = [self appInfoView];
39     view.appInfo = appInfo;
40     
41     return view;
42 }
43 
44 - (void)setAppInfo:(LWTAppInfoList *)appInfo
45 {
46     _appInfo = appInfo;
47     self.imageView.image = appInfo.image;
48     self.label.text = appInfo.name;
49 }
50 
51 
52 - (IBAction)downloadClick {
53     // 创建弹窗,提示下载完成
54     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80, 360, self.superview.bounds.size.width - 80 *2, 40)];
55     // 弹窗背景颜色
56     label.backgroundColor = [UIColor lightGrayColor];
57     
58     // 设置文字
59     LWTAppInfoList *appInfo = self.appInfo;
60     label.text = [NSString stringWithFormat:@"安装 %@ 成功",appInfo.name];
61     label.font = [UIFont systemFontOfSize:14.0];
62     label.textAlignment = NSTextAlignmentCenter;
63     label.textColor = [UIColor whiteColor];
64     
65     label.alpha = 0.0;
66     // 向UIView中添加弹窗
67     [self.superview addSubview:label];
68     
69     [UIView animateWithDuration:1.2 animations:^{
70         label.alpha = 1.0;
71         self.downloadButton.enabled = 0;
72         [self.downloadButton setTitle:@"已下载" forState:UIControlStateDisabled];
73     } completion:^(BOOL finished) {
74         [UIView animateWithDuration:1.0 animations:^{
75             label.alpha = 0.0;
76         } completion:^(BOOL finished) {
77             [label removeFromSuperview];
78         }];
79     }];
80 }
81 @end

plist文件:app.plist

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 3 <plist version="1.0">
 4 <array>
 5     <dict>
 6         <key>name</key>
 7         <string>天天酷跑</string>
 8         <key>icon</key>
 9         <string>icon_00</string>
10     </dict>
11     <dict>
12         <key>name</key>
13         <string>全民飞机大战</string>
14         <key>icon</key>
15         <string>icon_01</string>
16     </dict>
17     <dict>
18         <key>name</key>
19         <string>Flappy Bird</string>
20         <key>icon</key>
21         <string>icon_02</string>
22     </dict>
23     <dict>
24         <key>name</key>
25         <string>天天飞车</string>
26         <key>icon</key>
27         <string>icon_03</string>
28     </dict>
29     <dict>
30         <key>name</key>
31         <string>节奏大师</string>
32         <key>icon</key>
33         <string>icon_04</string>
34     </dict>
35     <dict>
36         <key>name</key>
37         <string>爸爸去哪儿</string>
38         <key>icon</key>
39         <string>icon_05</string>
40     </dict>
41     <dict>
42         <key>name</key>
43         <string>欢乐斗地主</string>
44         <key>icon</key>
45         <string>icon_06</string>
46     </dict>
47     <dict>
48         <key>name</key>
49         <string>天天爱消除</string>
50         <key>icon</key>
51         <string>icon_07</string>
52     </dict>
53     <dict>
54         <key>name</key>
55         <string>神庙逃亡2</string>
56         <key>icon</key>
57         <string>icon_08</string>
58     </dict>
59     <dict>
60         <key>name</key>
61         <string>欢乐麻将</string>
62         <key>icon</key>
63         <string>icon_09</string>
64     </dict>
65     <dict>
66         <key>name</key>
67         <string>保卫萝卜2</string>
68         <key>icon</key>
69         <string>icon_10</string>
70     </dict>
71     <dict>
72         <key>name</key>
73         <string>神偷奶爸</string>
74         <key>icon</key>
75         <string>icon_11</string>
76     </dict>
77 </array>
78 </plist>

xib文件:appInfoView.xib

 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 2 <document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5053" systemVersion="13C64" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
 3     <dependencies>
 4         <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
 5     </dependencies>
 6     <objects>
 7         <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
 8         <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
 9         <view contentMode="scaleToFill" id="C0v-yf-zPJ" customClass="LWTAppInfoView">
10             <rect key="frame" x="0.0" y="0.0" width="80" height="90"/>
11             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
12             <subviews>
13                 <imageView userInteractionEnabled="NO" tag="1" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="icon_00" translatesAutoresizingMaskIntoConstraints="NO" id="VRa-mS-qp6">
14                     <rect key="frame" x="0.0" y="0.0" width="80" height="50"/>
15                     <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
16                 </imageView>
17                 <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" tag="2" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ec6-JM-UpD">
18                     <rect key="frame" x="0.0" y="49" width="80" height="20"/>
19                     <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
20                     <fontDescription key="fontDescription" type="system" pointSize="12"/>
21                     <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
22                     <nil key="highlightedColor"/>
23                 </label>
24                 <button opaque="NO" tag="3" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="LY7-KP-ajm">
25                     <rect key="frame" x="15" y="70" width="50" height="20"/>
26                     <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
27                     <fontDescription key="fontDescription" type="system" pointSize="13"/>
28                     <state key="normal" title="下载" backgroundImage="buttongreen">
29                         <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
30                     </state>
31                     <state key="highlighted" backgroundImage="buttongreen_highlighted"/>
32                     <connections>
33                         <action selector="downloadClick" destination="C0v-yf-zPJ" eventType="touchUpInside" id="sDN-GQ-cZc"/>
34                     </connections>
35                 </button>
36             </subviews>
37             <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
38             <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
39             <connections>
40                 <outlet property="downloadButton" destination="LY7-KP-ajm" id="h4A-GG-XLX"/>
41                 <outlet property="imageView" destination="VRa-mS-qp6" id="DAr-Ir-Aa0"/>
42                 <outlet property="label" destination="ec6-JM-UpD" id="cVj-wx-BkW"/>
43             </connections>
44         </view>
45     </objects>
46     <resources>
47         <image name="buttongreen" width="193" height="81"/>
48         <image name="buttongreen_highlighted" width="193" height="81"/>
49         <image name="icon_00" width="65" height="65"/>
50     </resources>
51 </document>
原文地址:https://www.cnblogs.com/wentianblog/p/3748788.html