title: Allocate or Initialize Subclass From Superclass
body: <p>I've got a universal iOS app that I've been working on.  For the most part, I've been handling device differences by creating subclasses, so RootViewController is subclassed by RootViewController-iPad and RootViewController-iPhone.  The pattern starts in main.m, where I do device detection as follows:</p>
  
  <pre><code>int retVal;
  if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
      retVal = UIApplicationMain(argc, argv, nil, @"SalesPadAppDelegate_iPad");
  } else {
      retVal = UIApplicationMain(argc, argv, nil, @"SalesPadAppDelegate_iPhone");
  }
  </code></pre>
  
  <p>This pattern is working quite well, and is minimize code reuse.  </p>
  
  <p>Further down the stack, I've got a series of view controllers that inherit from a common superclass, MasterDetailViewController.  This superclass needs some device specific code, so I was thinking of subclassing that, and then ending up with the superclass automatically allocating or instantiating the device specific subclass that's most suitable.</p>
  
  <p>Implementation wise, this looks like the following:</p>
  
  <pre><code>+(id)allocWithZone:(NSZone *)zone {
      if ([[self class] isEqual:[MasterDetailViewController_iPad class]] || [[self class] isEqual:[MasterDetailViewController_iPhone class]]) {
          return [super allocWithZone:zone];
      } else {
          if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
              return [MasterDetailViewController_iPhone allocWithZone:zone];
          } else {
              return [MasterDetailViewController_iPad allocWithZone:zone];
          }
      }
  }
  </code></pre>
  
  <p>This code doesn't work, and is kind of a demonstration of concept.  Is there a way to do what I'm trying to do, or am I looking at things all wrong?</p>
  
  <p><strong>EDIT</strong>: Just to be clear, the data is the same between the super and subclasses.  The only difference would be a couple of method implementations.  The idea being that subclassing is cleaner than device specific spaghetti code.</p>