加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:28:26 所属栏目:百科 来源:网络整理
导读:参见英文答案 How to call original implementation when overwriting a method with a category?????????????????????????????????????3个 我想通过类似的类别向UIButton类添加一个新的自定义UIButtonType: enum { UIButtonTypeMatteWhiteBordered = 0x100
参见英文答案 > 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读