[iOS基礎控制項,ios基礎控制項
A.從ViewController分離View
之前的代碼中,View的資料載入邏輯放在了總的ViewController中,增加了耦合性,應該對控制器ViewController隱藏資料載入到View的細節。
封裝View的建立邏輯
封裝View的資料載入邏輯到自訂的UIView中
B.思路 使用xib封裝自訂view的步驟: 1.建立一個繼承UIView的自訂view,這裡的名字是“AppView”,用來封裝獨立控制群組 每個AppView封裝了如的控制群組 2.建立一個xib檔案來描述控制項結構,就是的控制群組 3.在Controller中使用AppView作為每個獨立控制群組的類型單位 4.將控制項和View “AppView” 進行連線 5.View “AppView” 提供一個模型屬性 6.重寫模型屬性的setter,解析模型資料 7.設定模型資料到控制項中 8.自訂View “AppView”的構造方法,屏蔽讀取xib檔案的細節 其實這就是一種簡單的MVC模式 Model: App.h, App.m View: AppView.h, AppView.m Controller: ViewController.h, ViewController.m Controller串連了View和Model,取得資料後載入到Model,然後傳給View進行解析並顯示 C.實現 1.建立UIView類”AppView",繼承自UIView new file ==> 建立聲明檔案”AppView.h”和“AppView.m” a. b. c. 2.設定xib的class (預設是UIView) 為建立的”AppView" 3.在建立的UIView中編寫View的資料載入邏輯 (1)在”AppView.h”中建立Model成員
1 // 在Controller和View之間傳輸的Model資料2 @property(nonatomic, strong) App *appData;
(2)串連控制項和”AppView",建立私人控制項成員 (3)在”AppView.m”中解析載入資料
1 - (void)setAppData:(App *)appData {2 _appData = appData;3 4 // 1.設定圖片5 self.iconView.image = [UIImage imageNamed:appData.icon];6 // 2.設定名字7 self.nameLabel.text = appData.name;8 } (4)自訂構造方法AppView.h:
1 // 自訂將Model資料載入到View的構造方法2 - (instancetype) initWithApp:(App *) appData;3 // 自訂構造的類方法4 + (instancetype) appViewWithApp:(App *) appData;5 // 返回一個不帶Model資料的類構造方法6 + (instancetype) appView;
AppView.m:
1 // 自訂將Model資料載入到View的構造方法 2 - (instancetype) initWithApp:(App *) appData { 3 // 1.從NIB取得控制項 4 UINib *nib = [UINib nibWithNibName:@"app" bundle:[NSBundle mainBundle]]; 5 NSArray *viewArray = [nib instantiateWithOwner:nil options:nil]; 6 AppView *appView = [viewArray lastObject]; 7 8 // 2.載入Model 9 appView.appData = appData;10 11 return appView;12 }13 14 // 自訂構造的類方法15 + (instancetype) appViewWithApp:(App *) appData {16 return [[self alloc] initWithApp:appData];17 }18 19 // 返回一個不帶Model資料的類構造方法20 + (instancetype) appView {21 return [self appViewWithApp:nil];22 }
(5)在Controller中建立”AppView”並載入資料
1 // 1.建立View 2 AppView *appView = [AppView appViewWithApp:appData]; 3 4 // 2.定義每個app的位置、尺寸 5 CGFloat appX = marginX + column * (marginX + APP_WIDTH); 6 CGFloat appY = marginY + row * (marginY + APP_HEIGHT); 7 appView.frame = CGRectMake(appX, appY, APP_WIDTH, APP_HEIGHT); 8 9 10 // 3.加入此app資訊到總view11 [self.view addSubview:appView];
主要代碼
1 ViewController.m 2 #import "ViewController.h" 3 #import "App.h" 4 #import "AppView.h" 5 6 #define ICON_KEY @"icon" 7 #define NAME_KEY @"name" 8 #define APP_WIDTH 85 9 #define APP_HEIGHT 90 10 #define MARGIN_HEAD 20 11 #define ICON_WIDTH 50 12 #define ICON_HEIGHT 50 13 #define NAME_WIDTH APP_WIDTH 14 #define NAME_HEIGHT 20 15 #define DOWNLOAD_WIDTH (APP_WIDTH - 20) 16 #define DOWNLOAD_HEIGHT 20 17 18 @interface ViewController () 19 20 /** 存放應用資訊 */ 21 @property(nonatomic, strong) NSArray *apps; // 應用列表 22 23 @end 24 25 @implementation ViewController 26 27 - (void)viewDidLoad { 28 [super viewDidLoad]; 29 // Do any additional setup after loading the view, typically from a nib. 30 31 [self loadApps]; 32 } 33 34 - (void)didReceiveMemoryWarning { 35 [super didReceiveMemoryWarning]; 36 // Dispose of any resources that can be recreated. 37 } 38 39 #pragma mark 取得應用列表 40 - (NSArray *) apps { 41 if (nil == _apps) { 42 // 1.獲得plist的全路徑 43 NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]; 44 45 // 2.載入資料 46 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; 47 48 // 3.將dictArray裡面的所有字典轉成模型,放到新數組中 49 NSMutableArray *appArray = [NSMutableArray array]; 50 for (NSDictionary *dict in dictArray) { 51 // 3.1建立模型對象 52 App *app = [App appWithDictionary:dict]; 53 54 // 3.2 添加到app數組中 55 [appArray addObject:app]; 56 } 57 58 _apps = appArray; 59 } 60 61 return _apps; 62 } 63 64 #pragma mark 載入全部應用列表 65 - (void) loadApps { 66 int appColumnCount = [self appColumnCount]; 67 int appRowCount = [self appRowCount]; 68 69 CGFloat marginX = (self.view.frame.size.width - APP_WIDTH * appColumnCount) / (appColumnCount + 1); 70 CGFloat marginY = (self.view.frame.size.height - APP_HEIGHT * appRowCount) / (appRowCount + 1) + MARGIN_HEAD; 71 72 int column = 0; 73 int row = 0; 74 for (int index=0; index<self.apps.count; index++) { 75 App *appData = self.apps[index]; 76 77 // 1.建立View 78 AppView *appView = [AppView appViewWithApp:appData]; 79 80 // 2.定義每個app的位置、尺寸 81 CGFloat appX = marginX + column * (marginX + APP_WIDTH); 82 CGFloat appY = marginY + row * (marginY + APP_HEIGHT); 83 appView.frame = CGRectMake(appX, appY, APP_WIDTH, APP_HEIGHT); 84 85 86 // 3.加入此app資訊到總view 87 [self.view addSubview:appView]; 88 89 column++; 90 if (column == appColumnCount) { 91 column = 0; 92 row++; 93 } 94 } 95 } 96 97 98 #pragma mark 計算資料行數 99 - (int) appColumnCount {100 int count = 0;101 count = self.view.frame.size.width / APP_WIDTH;102 103 if ((int)self.view.frame.size.width % (int)APP_WIDTH == 0) {104 count--;105 }106 107 return count;108 }109 110 #pragma mark 計算行數111 - (int) appRowCount {112 int count = 0;113 count = (self.view.frame.size.height - MARGIN_HEAD) / APP_HEIGHT;114 115 if ((int)(self.view.frame.size.height - MARGIN_HEAD) % (int)APP_HEIGHT == 0) {116 count--;117 }118 119 return count;120 }121 122 @end
AppView.m:
1 #import "AppView.h" 2 #import "App.h" 3 4 // 封裝私人屬性 5 @interface AppView() 6 7 // 封裝View中的控制項,只允許自己訪問 8 @property (weak, nonatomic) IBOutlet UIImageView *iconView; 9 @property (weak, nonatomic) IBOutlet UILabel *nameLabel;10 11 @end12 13 @implementation AppView14 15 - (void)setAppData:(App *)appData {16 // 1.賦值Medel成員17 _appData = appData;18 19 // 2.設定圖片20 self.iconView.image = [UIImage imageNamed:appData.icon];21 // 3.設定名字22 self.nameLabel.text = appData.name;23 }24 25 // 自訂將Model資料載入到View的構造方法26 - (instancetype) initWithApp:(App *) appData {27 // 1.從NIB取得控制項28 UINib *nib = [UINib nibWithNibName:@"app" bundle:[NSBundle mainBundle]];29 NSArray *viewArray = [nib instantiateWithOwner:nil options:nil];30 AppView *appView = [viewArray lastObject];31 32 // 2.載入Model33 appView.appData = appData;34 35 return appView;36 }37 38 // 自訂構造的類方法39 + (instancetype) appViewWithApp:(App *) appData {40 return [[self alloc] initWithApp:appData];41 }42 43 // 返回一個不帶Model資料的類構造方法44 + (instancetype) appView {45 return [self appViewWithApp:nil];46 }47 48 @end
App.m
1 #import "App.h" 2 3 #define ICON_KEY @"icon" 4 #define NAME_KEY @"name" 5 6 @implementation App 7 8 - (instancetype) initWithDictionary:(NSDictionary *) dictionary { 9 if (self = [super init]) {10 self.name = dictionary[NAME_KEY];11 self.icon = dictionary[ICON_KEY];12 }13 14 return self;15 }16 17 18 + (instancetype) appWithDictionary:(NSDictionary *) dictionary {19 // 使用self代表類名代替真實類名,防止子類調用出錯20 return [[self alloc] initWithDictionary:dictionary];21 }22 23 @end