Vytvoril som vlastnú tlačidlovú triedu s niektorými ďalšími vlastnosťami, ktoré zdedí z UIButtonu a pridáva sa do pohľadu, ako je to s nasledujúcim normálnym tlačidlom:
EFButton *btn = [EFButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(500, 100, 100, 44);
btn.backgroundColor = [UIColor lightGrayColor];
btn.userInteractionEnabled = NO;
[self.view addSubview:btn];
UIButton *nBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
nBtn.frame = CGRectMake(500, 100, 100, 44);
nBtn.backgroundColor = [UIColor lightGrayColor];
nBtn.userInteractionEnabled = NO;
[self.view addSubview:nBtn];
Prekrútené objekty, ako sú:
for (id view in self.view.subviews)
{
if ([view isKindOfClass:[EFButton class]])
{
//** dsnt come here **
NSLog(@"EFButton Class");
EFButton *btn = view;
}
if ([view isKindOfClass:[UIButton class]])
{
//** come here **
NSLog(@"UIButton Class");
UIButton *btn = view;
}
}
UIButton je práca, ale neprichádza do stavu isKindOfClass: [trieda EFButton]. Prosím, pomôžte mi s tým, čo robím zle alebo ako je potrebné urobiť.
odpovede:
5 pre odpoveď č. 1Je to preto, že buttonWithType
je trieda metódy UIButton
, vráti sa UIButton
typ, aj keď váš EFButton
dedí UIButton
, nemôže sa vrátiť EFButton
typ
Citovať vývojára Dokumenty
If you subclass UIButton, this method does not return an instance of your subclass
PS: Nemyslím, že zdedím UIButton
je dobrý spôsob.