第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

UIButton自定義屬性

UIButton自定義屬性

翻翻過去那場雪 2019-03-29 11:00:55
我要給UIButton自定義一個(gè)屬性,我這樣做的MyUIButton.h@interfaceMyUIButton:UIButton{NSString*idx;}@property(nonatomic,retain)NSString*idx;@endMyUIButton.m@implementationMyUIButton@synthesizeidx;@end#import"MyUIButton.h"MyUIButton*btn=((MyUIButton*)[MyUIButtonbuttonWithType:UIButtonTypeRoundedRect]);btn.idx=@"abcd";然后報(bào)錯(cuò)了:-[UIRoundedRectButtonsetIdx:]:unrecognizedselectorsenttoinstance0x816b2a0難道這樣擴(kuò)展屬性不對(duì)么?
查看完整描述

2 回答

?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

你代碼中雖繼承了UIbutton重寫了init,但是未重寫buttonWithType:,所以在調(diào)用[MyUIButtonbuttonWithType:UIButtonTypeRoundedRect]時(shí)實(shí)際上調(diào)用了父類的buttonWithType:,父類的buttonWithType:調(diào)用了某種UIButton的init。為什么我說是某種UIButton?因?yàn)閁IButton的buttonWithType:可以生成不同類型的對(duì)象,這些對(duì)象都是UIButton的子類。(當(dāng)然不可能生成MyUIButton類型的對(duì)象,也就無法響應(yīng)setIdx:方法)實(shí)際上,UIButton是一種聚類,你不能直接繼承它。應(yīng)當(dāng)增加擴(kuò)展,使用運(yùn)行時(shí)增加關(guān)聯(lián)對(duì)象。注意.m中引入了#import
:@interfaceUIButton(IdxProperty)
@property(nonatomic,retain)NSString*idx;
@end
#import
@implementationMyUIButton
@dynamicidx;
@end
-(NSString*)idx
{
NSString*idx=objc_getAssociatedObject(self,@"kUIButtonIdxKey");
returnidx;
}
-(void)setIdx:(NSString*)idx
{
objc_setAssociatedObject(self,@"kUIButtonIdxKey",idx,OBJC_ASSOCIATION_RETAIN);
}更干凈的寫法是給@"kUIButtonIdxKey"加個(gè)宏。此處我寫的有點(diǎn)dirty
樓主,還是那句話,加強(qiáng)下面向?qū)ο蟮膶W(xué)習(xí)
                            
查看完整回答
反對(duì) 回復(fù) 2019-03-29
?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊

-(id)buttonWithType:(UIButtonType)type
{
[superbuttonWithType:type];
self.idx=@"abcd";
}
                            
查看完整回答
反對(duì) 回復(fù) 2019-03-29
  • 2 回答
  • 0 關(guān)注
  • 372 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)