Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4331460
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:06:44+00:00 2026-05-21T10:06:44+00:00

I’m new to objective-C so, bear with me. I started with the Universal App

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:06:45+00:00Added an answer on May 21, 2026 at 10:06 am

    There are two simple ways to solve your problem. Both will work when placed in your superclass.

    The first method only works because you have two different classes, and which one is created depends on the device being used. It does not work if you don’t use different classes because there is no device-specific code. It involves asking the object for its class to decide which it is. Since the object’s class will be the device-specific class, even when asked for by the superclass, you can check to see which class was created and act accordingly.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Content *selectedContent = (Content *)[[self fetchedResultsController] objectAtIndexPath:indexPath];
        Class classToUse;
        if([self class] == [ExampleViewController_iPad class]) {
            // We are running on the iPad
            classToUse = [ContentDetailViewController_iPad class];
        } else {
            // We must be running on either the iPhone or iPod touch
            classToUse = [ContentDetailViewController_iPhone class];
        }
        // Just use superclass for typing here, since we aren't doing anything device specific
        ContentDetailViewController *detailViewController = [[classToUse alloc] init];
        detailViewController.content = selectedContent;
        detailViewController.managedObjectContext = self.managedObjectContext;
    
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
    }
    

    The second method will work in any location in your program. Apple added a property to the UIDevice class in iOS 3.2 called the “user interface idiom”. There are currently two possible values: UIUserInterfaceIdiomPad and UIUserInterfaceIdiomPhone. Since these don’t exist in versions prior to 3.2, Apple also added a macro which will return UIUserInterfaceIdiomPhone if the version is less than 3.2, and get the actual value from the UIDevice object if it is greater than or equal to 3.2.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Content *selectedContent = (Content *)[[self fetchedResultsController] objectAtIndexPath:indexPath];
        Class classToUse;
        // If you aren't supporting versions prior to 3.2, you can use [UIDevice currentDevice].userInterfaceIdiom instead to save a couple of cycles
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            // We are running on the iPad
            classToUse = [ContentDetailViewController_iPad class];
        } else {
            // We must be running on either the iPhone or iPod touch
            classToUse = [ContentDetailViewController_iPhone class];
        }
        // Just use superclass for typing here, since we aren't doing anything device specific
        ContentDetailViewController *detailViewController = [[classToUse alloc] init];
        detailViewController.content = selectedContent;
        detailViewController.managedObjectContext = self.managedObjectContext;
    
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.