I have an iPad 3 app that I want to only be viewable in landescape mode. I have a few UIViewContoller subclasses (it is a really simple app), and in all of them I have the following code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
The code is not big, but duplicated, and I would like to learn ways one can avoid this in Objective-C. I read about categories, and how they can override methods from classes / instances they apply to, so decided to try. I used the Xcode wizard to create a LandscapeOnly category for a UIViewController class, and moved the above implementation to it - to no avail. I tried adding the method signature to the header file, but this also didn't change anything.
How is this done? How can I use categories to override instance methods?
I read in another question here that categories and overriding methods is discouraged, so maybe I am wrong all along? What is the canonical way of achieving what I would like to achieve? I can use a custom UIViewController subclass, and have all other controllers derive from it, but this will break as soon as I have controllers with differing requirements, and hence multiple base classes. In other languages I would use mixins, for example, and I think Objective-c categories are their counterpart?