1

UIButton から継承するいくつかの追加のプロパティを持つカスタム ボタン クラスを作成しました。通常のボタンで次のようなビューに追加されます。

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];

次のようにビュー内のオブジェクトをループします。

  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は動作しますが、isKindOfClass:[EFButtonクラス]の状態では来ません。私が間違っていること、またはそれをどのように行う必要があるかを教えてください。

4

1 に答える 1

5

buttonWithTypeのクラスメソッドであるため、型をUIButton返しますが、継承していますが、 型を返すことはできません UIButtonEFButtonUIButtonEFButton

開発者ドキュメントを引用する

If you subclass UIButton, this method does not return an instance of your subclass

UIButtonPS: 継承は良い方法ではないと思います。

于 2013-01-28T05:09:45.787 に答える