开发者

Reusing codes between in two similar subclasses Objective C

I have a bit of code that I use to draw a border around a NSTextField when mousing over it, and while editing it, as well as to have it resize vertically when the user is typing. I want to use this code on an NSTokenFieldCell now. I've gotten it to work fine, and the implementation is identical for both NSTextField and NSTokenFieldCell. I was wondering if there was some way I could avoid the need to duplicate my code between the two class types. I believe NSToken开发者_JS百科FieldCell is actually a subclass of NSTextField.


I ended up pulling all my custom functionality out into a separate NSObject class, and then gave both my NSTextField and NSTokenField classes an instance of it, then I just proxied all of the custom logic into my "logic" object using - (id)forwardingTargetForSelector:(SEL)aSelector

It's not perfect as I still need to manually proxy over the methods that I overrode such as drawRect in each class, but I'd say it falls into the "good enough" category now. I'm interested to see if anyone has a better solution I can try.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜