objective-c ios: override implementation method of

2019-06-12 21:13发布

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条回答
ら.Afraid
2楼-- · 2019-06-12 21:48

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

查看更多
登录 后发表回答