iOS UI基礎學習 Note dayFour

2021-07-26 13:33:50 字數 2502 閱讀 9004

今天的知識點不多,在第三天的基礎上載入資料,懶載入,如何將字典轉換為模型,學習自定義控制項,還有構造方法的複習等等,今天學得比較吃力,對於乙個初學者來說稍微有點兒複雜

載入資料的方式有多種,比如用陣列..if..switch..字典..陣列+字典等.

我這裡只介紹最後一種,將陣列和字典寫成乙個.plist檔案,例如

nsarray *arr = @[

@,@,

][arr writetofile: atomically:];

//writetofile 填寫檔案生成路徑,atomically yes:寫入不成功不會生成檔案 no:寫入失敗時也會生成檔案

將生成的.plist檔案載入到supporting files中,這個時候我們就可以使用它了,下圖是完善購物車demo的.plist檔案

接下來是將plist檔案中的資料取出(將字典轉換為模型)

@property (nonatomic, strong) nsarray *dataarr;

//這裡要注意 這是重寫getter方法

- (nsarray *)dataarr

self

.dataarr = dataarrm;

//最後再一次賦值一次給全域性變數陣列

}return _dataarr;

}

shopview *shopview = [[shopview alloc]initwithshopdata:self.dataarr[index]];
因為最後的賦值是self.dataarr 所以呼叫了getter/setter方法 這個時候,上面強調為什麼是重寫getter方法

上面的**還用到了構造方法(複習)和字典轉換模型方法

@inte***ce

shopdata : nsobject

@property (nonatomic,copy) nsstring * name;

//建立乙個全域性變數來接收plist檔案中的name

@property (nonatomic,copy) nsstring *icon;

//建立乙個全域性變數來接收plist檔案中的icon

//構造方法

- (instancetype)initwithdict:(nsdictionary *)dict;

+ (instancetype)shopdatawithdict:(nsdictionary *)dict;

@end

@implementation

shopdata

- (instancetype)initwithdict:(nsdictionary *)dict

return

self;

/*將接受到的字典賦值給資料類用類,並賦值給資料類的全域性變數

返回資料*/}

+ (instancetype)shopdatawithdict:(nsdictionary *)dict

@end

demo中的商品包含乙個uiimageview和乙個uilabel 所以自定義乙個控制項包含他們,這個時候建立乙個類,繼承與uiview,**如下

@property (nonatomic, strong) shopdata *shopdata;

//構造方法

- (instancetype)initwithshopdata:(shopdata *)data;

+ (instancetype)shopwithshopdata:(shopdata *)data;

@end

#import "shopdata.h"

@inte***ce

shopview ()

//定義兩個控制項的全域性變數

@property (nonatomic, weak) uiimageview *iconview;

@property (nonatomic, weak) uilabel *nameview;

@end

@implementation

shopview

- (instancetype)init

return

self;

}- (void)initialize

-(void)layoutsubviews

- (void)setshopdata:(shopdata *)shopdata

//接受資料 把資料類傳入自定義控制項類

- (instancetype)initwithshopdata:(shopdata *)data

return

self;

}+ (instancetype)shopwithshopdata:(shopdata *)data

初八 繼續學習!

iOS UI基礎學習 Note dayThree

今天的內容不多,主要是對uibutton的乙個深入了解,用 做乙個uibutton 和利用之前的知識點做乙個模擬購物車的乙個demo 用 實現建立uibutton並監聽按下的動作 demo 核心 如下 void button void monitor 效果如下 按下去後會呼叫monitor方法 按下...

iOS UI基礎知識

ios ui開發中有許多基礎知識需要了解,如 uidevice uiscreen uiwindow ios座標系 cgpoint cgrect frame bounds uicolor uiview tag hide,下面就來分別說明一下 如下 uidevice device uidevice cu...

iOS UI基礎 03按鈕操作

import hmviewcontroller.h inte ce hmviewcontroller property weak,nonatomic iboutlet uibutton iconbutton end implementation hmviewcontroller 1.基本功能實現 2...