I'm new to objective-C so, bear with me. I started with the Universal App template in Xcode4 and built my application. There is a convention that the template starts you off with that I tried to stick with. For each View Controller, there's a main file and a subclass for each device type. For example:
Project/
ExampleViewController.(h|m)
- iPhone/
- ExampleViewController_iPhone.(h|m|xib)
- iPad/
- ExampleViewController_iPad.(h|m|xib)
For the most part, this is pretty convenient. Most of the logic goes in the superclass and the subclasses take care of any device specific implementation.
Here's the part I don't get. Sometimes I have code that does the same thing in each subclass simply because I need to load a different xib for each device. For example:
ExampleViewController_iPhone
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Content *selectedContent = (Content *)[[self fetchedResultsController] objectAtIndexPath:indexPath];
ContentDetailViewController_iPhone *detailViewController = [[ContentDetailViewController_iPhone alloc] init];
detailViewController.content = selectedContent;
detailViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
ExampleViewController_iPad
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Content *selectedContent = (Content *)[[self fetchedResultsController] objectAtIndexPath:indexPath];
ContentDetailViewController_iPad *detailViewController = [[ContentDetailViewController_iPad alloc] init];
detailViewController.content = selectedContent;
detailViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
... notice that in the second instance the only thing different is that it's loading the _iPad
version of the View Controller. This is necessary because the iPad
and iPhone
view controllers are attached to separate, device specifc nibs.
What's the "correct" pattern for doing this?
UPDATE
I found this answer about loading separate xibs using device modifiers which seems like it could help in the case where I don't need a specific subclass for one device, but it still won't help if I need to instantiate a specific _iPhone
or _iPad
instance of a view controller for device specific functionality.