Archive for the ‘Obj-c’ Category

Extending(Subclassing) UIButton in Objective-C

Friday, July 10th, 2009

After playing around with UIButton class for a while I found Apple have been very sneaky with some  of their UI classes.

What it appears they have done is use the UIButton class as a sort of wrapper for a number of private button classes. “UIRoundedRectButton” is one of those classes.

“UIRoundedRectButton” is a private class so it can not be extended(subclassed). The initilizers normaly return an instance of self and if that self is not the class type trying to hold it there will be trouble. The problem is if you initialise UIButton with buttonWithType the class it returns is a private class. If you try and extended the UIButton once your custom class is initialized it returns a “UIRoundedRectButton” object which is private so you can not contain it.

If you try the code below which uses the description you can see the object description is not UIButton.

[c]UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSLog(@"myButton type: %@", [myButton description]);[/c]

In short you can not extend UIButton and initialize it with buttonWithType  in Objective C. On the other hand you can extened UIButton for a button in interface builder. If  you use interface builder you need to use initWithCoder as the initialiser. While interface builder does the private stuff.

Stackoverflow Inspiration and Learning Event

Friday, July 3rd, 2009

The popular code question and answer site http://stackoverflow.com/ are having a few inspiration and learning events this year. I am keen to see what android and objective-c stuff will be on offer you can get tickets from: http://stackoverflow.carsonified.com/