objective-c ios: override implementation method of class

大兔子大兔子 提交于 2019-12-23 05:37:07

问题


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

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

I'm extending DropdownMenuController in my own MyDropdownMenuController:

MyDropdownMenuController.h

#import "DropdownMenuController.h"

@interface MyDropdownMenuController : DropdownMenuController

@end

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.?


回答1:


You can create a category that defines the method

@interface DropdownMenuController (MichaelHacksIt)

- (void)drawOpenLayer;

@end

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

Then you may just call that method.

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...



来源:https://stackoverflow.com/questions/23372454/objective-c-ios-override-implementation-method-of-class

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