Subclassing UIButton but can't access my properties

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

I\'ve created a sub class of UIButton:

//
//  DetailButton.h
#import 
#import 

@interface MyDetailButt         


        
4条回答
  •  庸人自扰
    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 :-)

提交回复
热议问题