Search code examples
iosobjective-coverridingobjective-c-category

Can't call the category method by selector from the main class


I have a method:

+ (id) showModalFromController: (UIViewController*) controller
{
    AxEmpAuthorizationController * autorizationController = [[self.class alloc] initWithNibName:NSStringFromClass(self.class) bundle:nil];
    [autorizationController performSelectorOnMainThread: @selector(showModalFromController:) withObject: controller waitUntilDone: YES];
    return [autorizationController authorelease];
}

and I had a category AxEmpAuthorizationController+CustomLoginVC.h in which I have override the method:

- (void) showModalFromController: (UIViewController*) controller
{
    NavigationTopViewController* navigationController = [[NavigationTopViewController allocWithZone: NULL] initWithRootViewController: self];
    [controller presentModalViewController: navigationController animated: ![self.class isMain]];
    [navigationController release];
}

The problem is that the method in category is never called, and I can't find the issue. Any help?


Solution

  • One issue here is you aren't overriding the method. The selectors will be the same (I believe) - @selector(showModalFromController:), but the method is not. For one they have different return types, another is one is a class method (starts with '+') and once is an instance method (starts with '-').

    You need to make sure your replacement method has the signature:

    + (id) showModalFromController: (UIViewController*) controller
    

    rather than

    - (void) showModalFromController: (UIViewController*) controller
    

    Once you've sorted this out, you're one step closer.

    As rmaddy commented above, you probably don't want to use a category to override a method.

    If you're trying to replace behavior, consider subclassing and using your new subclass in the places you need it.

    If you're trying to do something stealthy - replacing this method throughout your application, consider method swizzling (consider this option very carefully).