Objective-C iOS:重写类的实现方法 [英] objective-c ios: override implementation method of class

查看:393
本文介绍了Objective-C iOS:重写类的实现方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手,对这样的问题有些困惑.

I'm new to iOS development and a bit stucked with such problem.

在我的iPhone应用程序中,我正在使用这个很棒的下拉视图控制器 https://github.com/nmattisson/DropdownMenu 通过Cocoapods .

In my iphone app I'm using this awesome dropdown view controller https://github.com/nmattisson/DropdownMenu via Cocoapods.

我在自己的MyDropdownMenuController中扩展DropdownMenuController:

MyDropdownMenuController.h

#import "DropdownMenuController.h"

@interface MyDropdownMenuController : DropdownMenuController

@end

我想覆盖此drawOpenLayer(( https:我的控制器实例中的//github.com/nmattisson/DropdownMenu/blob/master/DropdownMenu/DropdownMenuController.m#L126 )方法,但不幸的是,编译器说这是不可能的:

I would like to override this drawOpenLayer (https://github.com/nmattisson/DropdownMenu/blob/master/DropdownMenu/DropdownMenuController.m#L126) method inside my controller instance, but unfortunately compiler says it's not possible:

MyDropdownMenuController.m

- (void)drawOpenLayer {
   // compiler says 
   // "No visible @interface for "DropdownMenuController" declares the selector "drawOpenLayer"
   [super drawOpenLayer];
}

是否可以在不实际更新外部接口等的情况下覆盖此方法?

Is it possible to override this method without actually updating external interface etc.?

推荐答案

您可以创建定义方法的类别

You can create a category that defines the method

@interface DropdownMenuController (MichaelHacksIt)

- (void)drawOpenLayer;

@end

(无需为此定义@implementation,因为它已经实现了.)

(no need to define the @implementation for this, because it's already implemented.)

然后您可以调用该方法.

Then you may just call that method.

免责声明: 顺便说一句,如果您要调用未公开记录的方法并且不关心Apple的批准,那么这就是要走的路.在这种情况下,它没有任何问题,因为您不是在破解Apple代码,并且Apple不在乎您是否在破解某些CocoaPods程序.但是,您依赖于第三方程序包的内部,因此下次更新该程序包时可能会出现问题...

Disclaimer: Btw, that's the way to go if you want to call undocumented methods and don't care about Apples approval. In this case, there is nothing wrong with it, because you're not hacking Apple code, and Apple doesn't care if you hack some CocoaPods program. However, you are depending on internals of a third-party package, so there may be problems when you update that package next time...

这篇关于Objective-C iOS:重写类的实现方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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