How do I call the original function from the overloaded function in a category?

六眼飞鱼酱① 提交于 2019-12-07 07:02:30

问题


In Objective-C, I have a category for a class:

@interface UILabel(CustomInit)

- (id)initWithCoder:(NSCoder *)coder;

@end

What I'm doing is writing a custom init function that does some extra stuff, and what I'd like to do, is in this custom init function, call the UILabel's base initWithCoder. Is this possible? How so?

EDIT

Thanks. Ok, so my plans moot. Can't just overload initWithCoder. Is there a way to achieve the same functionality (where all UILabels get this added initialization step) without overloading initWithCoder? Or perhaps is there sample code for the UILabel's initWithCoder that I can just rewrite with the added code?

EDIT

Ok, so to be clear about what I'm trying:

Can I embed a custom font in an iPhone application?

has an answer in which someone manually adds a custom font on the iphone using the private GraphicServices function GSFontAddFromFile. I tried this code and it worked great for manually setting the font of a label. However, if you try setting the font in Interface Builder, it doesn't load properly, it just drops down to the system font. What I wanted to do was load the font manually and set the label's font automatically with the chosen font in IB. This way I don't need to make an outlet for every label I put down. I also don't have to write a ridiculous label subclass (which was also suggested in that thread and does a large amount of custom drawing) which I found rather grotesque. Now I could still make a subclass for all my labels, but then there's the case of embedded labels in other UI objects, ie UIButtons. I'd like the embedded labels to also not be broken.

Any suggestions would be great. Thanks.


回答1:


From the Mac OS X Reference Library:

When a category overrides an inherited method, the method in the category can, as usual, invoke the inherited implementation via a message to super. However, if a category overrides a method that already existed in the category's class, there is no way to invoke the original implementation.




回答2:


How do you guys feel about this?

Grab the original method address for initWithCoder at runtime and store it in a static variable. Do a method swizzle on it to replace the classes implementation with the my initWithCoder. And then in my initWithCoder, I would call the original method stored in the static variable.

You can put it in a category and call this class initialization step at the start of the program, making sure it can't be called twice, or if it is it does nothing.

It seems dangerous, but I feel like it should work.




回答3:


Method swizzling should work as kidnamedlox suggested .

Your exact same question was discussed in this Stanford itunes class by Evan Doll

https://podcasts.apple.com/us/podcast/iphone-application-programming-spring-2009/id384233222



来源:https://stackoverflow.com/questions/1514692/how-do-i-call-the-original-function-from-the-overloaded-function-in-a-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!