I have created a custom class called RCTextField, whose purpose is to create an NSTextField with rounded corners (thus RC...).
This RCTextField inherits from NSTextField and overrides the drawRect: method in order to create its own rectangle with rounded corners.
Now, I want to do the same thing for NSSecureTextField, that is, have a way to make it have rounded corners. I can’t just make it inherit RCTextField, because that one doesn’t have any secure implementation for text that NSSecureTextField has.
And if I inherit from NSSecureTextField, I’d have to rewrite all the drawRect: implementation again.
An option I thought about would be to have an RCBaseTextField class which contains a static method called drawRect:forNSTextField:withParams:, and call that one in the drawRect: of RCTextField and RCSecureTextField. However, that seems a bit hackish, and it feels like there could be a better OOP way to do this in Objective-C.
So, what would be the best/sanest/software-engineering-“esquest” way to share the draw-rounded-corners code from RCTextField between an RCTextField and an RCSecureTextField?
Unfortunately, you cannot make an Objective-C inherit behavior from more that one other class. I think providing a function (using a Object-Oriented language doesn’t prevent from using functions when they are appropriate) that will be invoked by both
drawRect:implementation is the simpler way.Or you may just ignore
NSSecureTextFieldand makeRCSecureTextFieldinherit fromRCTextField. Then implement the class methodcellClassto returnNSSecureTextFieldCelland you should get the same exact features asNSSecureTextField.@interface RCSecureTextField : RCTextField @end @implementation RCSecureTextField + (Class)cellClass { return [NSSecureTextFieldCell class]; } @end