objective-c – 在重写类方法中调用super

objective-c – 在重写类方法中调用super,第1张

概述参见英文答案 > How to call original implementation when overwriting a method with a category?                                    3个 我想通过类似的类别向UIButton类添加一个新的自定义UIButtonType: enum { UIButtonTypeMatteWhit 参见英文答案 > How to call original implementation when overwriting a method with a category?                                    3个
我想通过类似的类别向UIbutton类添加一个新的自定义UIbuttonType:

enum {    UIbuttonTypeMatteWhitebordered = 0x100};@interface UIbutton (Custom)+ (ID)buttonWithType:(UIbuttonType)buttonType;@end

是否有可能以某种方式获得该重写方法的超级实现?

+ (ID)buttonWithType:(UIbuttonType)buttonType {    return [super buttonWithType:buttonType];}

上面的代码无效,因为super在此上下文中引用了UIControl.

解决方法 不,当您使用类别来扩充类的功能时,这是不可能的,您没有扩展类,实际上完全覆盖现有方法,您完全失去了原始方法.像风一样.

如果您创建UIbutton的子类,那么这是完全可能的:

enum {    UIbuttonTypeMatteWhitebordered = 0x100};@interface MyCustombutton : UIbutton {}@end@implementation MyCustombutton + (ID)buttonWithType:(UIbuttonType)buttonType {    return [super buttonWithType:buttonType]; //super here refers to UIbutton}@end
@H_419_39@ 总结

以上是内存溢出为你收集整理的objective-c – 在重写类方法中调用super全部内容,希望文章能够帮你解决objective-c – 在重写类方法中调用super所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1220442.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存