如何从类别中的重载函数调用原始函数? [英] How do I call the original function from the overloaded function in a category?

查看:207
本文介绍了如何从类别中的重载函数调用原始函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,我有一个类的类别:

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

@interface UILabel(CustomInit)

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

@end

我所做的是编写一个自定义init函数做一些额外的东西,我想做的是,在这个自定义init函数,调用UILabel的基本initWithCoder。这可能吗?如何?

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?

编辑

好吧,所以我的计划moot。不能只是重载initWithCoder。有没有办法实现相同的功能(所有UILabels得到这个添加的初始化步骤),而不重载initWithCoder?或者也许有UILabel的initWithCoder的示例代码,我可以用添加的代码重写?

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?

编辑

好,以便清楚我正在尝试什么:

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

我可以在iPhone应用程序中嵌入自定义字体吗?

有一个答案,有人手动添加自定义字体在iphone使用私人GraphicServices函数GSFontAddFromFile。我试过这个代码,它工作伟大的手动设置标签的字体。但是,如果您尝试在Interface Builder中设置字体,它不会正确加载,它只是下降到系统字体。我想要做的是手动加载字体并自动使用IB中选择的字体设置标签的字体。这样我不需要为每一个放下的标签做出口。我也不必写一个荒谬的标签子类(这也被建议在那个线程,并做了大量的自定义绘图),我发现很奇怪。现在我仍然可以为我的所有标签,一个子类,但是在其他UI对象,即UIButtons嵌入标签的情况。我想嵌入的标签也不会被打破。

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.

任何建议都会很棒。

推荐答案

你们对此有何感想?

在运行时获取initWithCoder的原始方法地址,并将其存储在静态变量中。做一个方法swizzle它用我的initWithCoder替换类的实现。然后在我的initWithCoder中,我将调用存储在静态变量中的原始方法。

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.

这篇关于如何从类别中的重载函数调用原始函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆