8.ios之view的封裝,8.ios之view封裝

來源:互聯網
上載者:User

8.ios之view的封裝,8.ios之view封裝

1.view的封裝

•如果一個view內部的子控制項比較多,一般會考慮自訂一個view,把它內部子控制項的建立屏蔽起來,不讓外界關心••外界可以傳入對應的模型資料給view,view拿到模型資料後給內部的子控制項設定對應的資料

2.使用xib封裝一個自訂view的步驟


1> 建立一個繼承UIView的自訂view,假設類名叫做(MJAppView)
2> 建立一個MJAppView.xib檔案來描述MJAppView內部的結構
3> 修改UIView的類型為MJAppView真是類型
4> 將內部的子控制項跟MJAppView進行屬性連線
5> MJAppView提供一個模型屬性
6> 重寫模型屬性的set方法,因為在set方法中可以拿到外界傳遞的模型資料
7> 把模型資料拆開,分別設定資料到對應的子控制項中
8> 補充:提供一個建立MJAppView的類方法,將讀取xib檔案的代碼屏蔽起來


3.Sample

#import <UIKit/UIKit.h>@class MJApp;@interface MJAppView : UIView/** *  模型資料 */@property (nonatomic, strong) MJApp *app;+ (instancetype)appView;/** *  通過模型資料來建立一個view */+ (instancetype)appViewWithApp:(MJApp *)app;@end#import "MJAppView.h"#import "MJApp.h"@interface MJAppView()@property (weak, nonatomic) IBOutlet UIImageView *iconView;@property (weak, nonatomic) IBOutlet UILabel *nameLabel;@end@implementation MJAppView//+ (instancetype)appView//{//    NSBundle *bundle = [NSBundle mainBundle];//    // 讀取xib檔案(會建立xib中的描述的所有對象,並且按順序放到數組中返回)//    NSArray *objs = [bundle loadNibNamed:@"MJAppView" owner:nil options:nil];//    return [objs lastObject];//}////+ (instancetype)appViewWithApp:(MJApp *)app//{//    MJAppView *appView = [self appView];//    appView.app = app;//    return appView;//}+ (instancetype)appViewWithApp:(MJApp *)app{    NSBundle *bundle = [NSBundle mainBundle];    // 讀取xib檔案(會建立xib中的描述的所有對象,並且按順序放到數組中返回)    NSArray *objs = [bundle loadNibNamed:@"MJAppView" owner:nil options:nil];    MJAppView *appView = [objs lastObject];    appView.app = app;    return appView;}+ (instancetype)appView{    return [self appViewWithApp:nil];}- (void)setApp:(MJApp *)app{    _app = app;        // 1.設定表徵圖    self.iconView.image = [UIImage imageNamed:app.icon];        // 2.設定名稱    self.nameLabel.text = app.name;}@end


#import "MJViewController.h"#import "MJApp.h"#import "MJAppView.h"@interface MJViewController ()/** 存放應用資訊 */@property (nonatomic, strong) NSArray *apps;@end@implementation MJViewController- (void)viewDidLoad{    [super viewDidLoad];        // 添加應用資訊        // 0.總列數(一行最多3列)    int totalColumns = 3;        // 1.應用的尺寸    CGFloat appW = 85;    CGFloat appH = 90;        // 2.間隙 = (控制器view的寬度 - 3 * 應用寬度) / 4    CGFloat marginX = (self.view.frame.size.width - totalColumns * appW) / (totalColumns + 1);    CGFloat marginY = 15;        // 3.根據應用個數建立對應的框框(index 0 ~ 11)    for (int index = 0; index<self.apps.count; index++) {        // 3.1.建立view        MJAppView *appView = [MJAppView 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);                // 3.4.設定資料//        appView.app = self.apps[index];    }}- (NSArray *)apps{    if (_apps == nil) {        // 初始化                // 1.獲得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.建立模型對象            MJApp *app = [MJApp appWithDict:dict];                        // 3.2.添加模型對象到數組中            [appArray addObject:app];        }                // 4.賦值        _apps = appArray;    }    return _apps;}@end


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.