Subclassing UIButton but can't access my properties

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

I\'ve created a sub class of UIButton:

//
//  DetailButton.h
#import 
#import 

@interface MyDetailButt         


        
相关标签:
4条回答
  • 2020-12-16 09:07

    Just making a subclass isn't enough; the subclass does not take the place of its superclass. The same way not all UIControls, not all UIViews, not all UIResponders, and not all NSObjects have the behavior of UIButton, not all UIButtons have the behavior of your custom subclass.

    What you need is an instance of your subclass. What you have is an instance of UIButton.

    The solution is to make that instance an instance of your subclass instead. If you created the button in Interface Builder, select the button and press ⌘6, then change the instance's Custom Class there. If you're creating the button in code, send your alloc message to your custom subclass, not directly to UIButton.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-16 09:20

    I made the same attempt as original poster but it seems that subclassing UIButton to do something like this is hard.

    What I did, which is a hack - but works for me now, is add a UITextField as subview of the UIButton. The UITextField has no frame and is hidden, but I can freely store text strings in the textfield's text property. Which is what I wanted to do...

    UITextField* tf = [[UITextField alloc] init];
    tf.text = @"the text that I wanna store";
    tf.hidden = YES;
    tf.tag = TAGOFBUTTONSUBTEXTFIELD;
    [previouslyCreatedButton addSubview:tf];
    [tf release];
    

    I defined TAGOFBUTTONSUBTEXTFIELD as 99 somewhere. Global. It's ugly but...

    Then later, to get that text string use something like this:

    +(NSString*)getStoredStringFromButton:(UIButton*)button {
        UITextField* tf = (UITextField*)[button viewWithTag:TAGOFBUTTONSUBTEXTFIELD];
        return tf.text;
    }
    

    So this assumes that no one else tries to add a subview with tag 99 to the button.

    Lol :-)

    0 讨论(0)
  • 2020-12-16 09:22

    That exception is because the actual button that you are trying to get the annotation from is not of class MyDetailButton, it is a UIButton. Verify that you set the class in IB for that particular button. Select the button in IB and press ⌘4 to see its identity, change the Class Identity to MyDetailButton.

    0 讨论(0)
提交回复
热议问题