I made the same attempt as the original poster, but it seems that subclassing UIButton to do something like this is complicated.
What I did was hack, but now it works for me, adds a UITextField as a UIButton subitem. UITextField has no frame and is hidden, but I can freely store text strings in the text property of a text field. This 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. This is ugly, but ...
Then, to get this 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 is trying to add a preview with tag 99 to the button.
Lol :-)
Jonny source share