0

I have created a category of UIViewController to take care of some view controller oddities in iOS 7. Without disclosing any APIs under NDA here is the overridden init method:

@implementation UIViewController (ios7)
-(id)init {
    self = [super init];
    if (self) {
        if ([self respondsToSelector:@selector(<iOS7OnlyMethod>)]) {
           // additional setup
        }
    }
    return self;
}
@end

I verified that the condition fires when running in 7.0 simulator, and doesn't fire if run in 6.1 simulator.

Here is the puzzle: although the code is not run, the tab bar icons of my tab bar disappear in 6.1. However, if I uncheck my target in the file inspector of this .m file (i.e. I do not include it in the compilation), the tab bar items appear as usual.

How is this possible?

EDIT

After some thinking about this I figured out that I am probably overriding the init method of UIViewController - so each UIViewController is initialised only with the init of UIResponder and the additional code (or not) that I provide. Presumably, UIViewController has much more and critical things in its init method which I have circumvented in this way.

The problem is, I have a huge project with many view controllers. It should work in iOS 6 and iOS 7. I thought with a category, I can avoid amending all view controllers in the project.

How to solve this? Is there maybe another method that can be overridden instead?

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • *Replacing* an existing method in a class category is indeed undefined behaviour: It is unspecified which implementation (the original or from the extension) is called. There is also no way of calling the original method from the extension method. - An alternative is the "method swizzling". – Martin R Sep 12 '13 at 07:21
  • That's what I thought. Could you explain "method swizzling"? Anyway, I am reading [this](http://stackoverflow.com/a/8636521/427083). – Mundi Sep 12 '13 at 07:49
  • This seems to be a good reference: http://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html. – Martin R Sep 12 '13 at 07:54

0 Answers0