Subclassing UIButton but can't access my properties

后端 未结 4 1870
夕颜
夕颜 2020-12-16 08:51

I\'ve created a sub class of UIButton:

//
//  DetailButton.h
#import 
#import 

@interface MyDetailButt         


        
4条回答
  •  时光取名叫无心
    2020-12-16 09:11

    UIButton is a class cluster, which implies that Apple's implementation of buttonWithType: probably looks something like this:

    +(id)buttonWithType:(UIButtonType)t {
      switch (t) {
        case UIButtonTypeDetailDisclosure:
          return [[[PrivateDetailDisclosureButtonClass alloc] init] autorelease];
        case ...
      }
    }
    

    So when you call [MyDetailButton buttonWithType:UIButtonTypeDetailDisclosure]; you don't get an instance of MyDetailButton, you get an instance of PrivateDetailDisclosureButtonClass (or whatever Apple actually calls it).

    Note, however, that you can get buttonWithType to instantiate a subclass if you call it with UIButtonTypeCustom (At least in the simulator running v3.0):

    // LGButton is a straightforward subclass of UIButton
    LGButton *testBtn = [LGButton buttonWithType:UIButtonTypeCustom];
    LGButton *testBtn2 = [LGButton buttonWithType:UIButtonTypeDetailDisclosure];
    NSLog(@"testBtn: %@, testBtn2: %@", [testBtn class], [testBtn2 class]);
    // Output: testBtn: LGButton, testBtn2: UIButton
    

提交回复
热议问题