好處:
@inte***ce
viewcontroller ()
@property (nonatomic, strong)nsarray *shops;
@end
@implementation
viewcontroller
// 重寫getter方法
- (nsarray *)shops
, @,@,@
];}
return _shops;
}@end
獲取plist檔案的絕對路徑
nsbundle *bundle = [nsbundle mainbundle];
nsstring *path = [bundle pathforresource:@"shops" oftype:@"plist"];
// 根據路徑載入plist檔案
_shop = [nsarray arraywithcontentoffile:path];
注意點: 檔名稱中不能包含info單詞
模型的優點
注意:
+ (instancetype)shopwithdict:(nsdictionary *)dict
模型的類名命名注意:
步驟:類工廠方法(便利構造器)
注意點:
layoutsubviews
作用:
- (void)layoutsubviews
注意點:
重寫set方法,設定子控制項資料
- (void)setshop:(njshop *)shop
// init方法內部會呼叫initwithframe
- (instancetype)initwithframe:(cgrect)frame
return
self;
}
注意: 一般情況下自定義乙個控制項會重寫控制項的initwithframe方法, , 因為使用者可能通過init方法建立也可能通過initwithframe方法建立, 為了保證無論使用者通過哪乙個方法建立都能新增子控制項, 所以重寫initwithframe
njshopview *shopview = [njshopview shopviewwithshop: self.shops[index]];
// njshopview *shopview = [[njshopview alloc] initwithshop:self.shops[index]];
shopview.backgroundcolor = [uicolor redcolor];
shopview.frame = cgrectmake(shopx, shopy, 70, 100);
[self.shopsview addsubview:shopview];
// 注意:自定義構造方法中的with的w一定要大寫
- (instancetype)initwithshop:(njshop *)shop;
+ (instancetype)shopviewwithshop:(njshop *)shop;
- (instancetype)initwithshop:(njshop *)shop
return
self;
}+ (instancetype)shopviewwithshop:(njshop *)shop
xib和storyboard對比
不同點如何載入xib
nsarray *views = [[nsbundle mainbundle] loadnibnamed:@"xib檔名" owner:nil options:nil]
// 注意: 建立uinib物件時需要傳入乙個bundle, 如果是去mainbundle中查詢, 那麼可以直接傳乙個nil
uinib *nib = [uinib nibwithnibname:@"xib檔名" bundle:nil];
nsarray *views = [nib instantiatewithowner:nil options:nil];
規律**示例:
// 1.載入xib
xmgshopview *shopview = [[[nsbundle mainbundle] loadnibnamed:@"xmgshopview" owner:nil options:nil] firstobject];
// 2.設定frame
cgrect tempframe = shopview.frame
; tempframe.origin
.x = shopx;
tempframe.origin
.y = shopy;
shopview.frame = tempframe;
// 3.設定資料
njshop *shop = self.shops[index];
shopview.iconview
.image = [uiimage imagenamed:shop.icon];
shopview.namelabel
.text = shop.name
;// 新增子控制項
[self.shopsview addsubview:shopview];
+ (instancetype)shopview
在」建立後」會呼叫awakefromnib
xmgshopview *shopview =[xmgshopview alloc] init];
shopview.backgroundcolor = [uicolor redcolor];
shopview.frame = cgrectmake(0, 0, 70, 100);
uiimageview *iv = [[uiimageview alloc] init];
iv.frame = cgrectmake(0, 0, 70, 70);
uilabel *label = [[uilabel alloc] init];
label.frame = cgrectmake(0, 70, 70, 30);
self.iconview = iv;
self.namelabel = label;
[shopview addsubview:iv]
;[shopview addsubview:label]
;
iOS開發之UI基礎 KVC
k vc key value coding 1.字典轉模型 dic answer dic icon dic title dic options kvc 把字典中的值,賦給當前物件制定的屬性 answer self setvalue dic answer forkeypath answer 遍歷字典中...
IOS開發UI基礎 倒影
1.用複製圖層實現,搞個uiimageview展示,然後複製uiimageview.2.注意 複製圖層只能複製子層,但是uiimageview只有乙個主層,並沒有子層,因此不能直接複製uiimageview.3.正確做法 應該把uiimageview新增到乙個uiview上,然後複製uiview的層...
iOS開發系列 UI基礎 KVC
這些知識是ui初級學習的,目前我還在學習中,適合初學者看 kvc key value coding 也就是鍵值編碼 是一種獲取值和設定值的方式 當我們建立乙個類檔案,為這個類設定成員屬性的時候 建立乙個 人 類 有名字 name 年齡 age 兩個公有的成員屬性 因為使用 property關鍵字,會...