Return to WrightsCS.com   |   Check out WrightsCSApps.com   |   How to Post Snippets  

Not registered? Thats okay! You can Submit a Snippet without being a member of this site!


          0 votes

I like to use 1 controller for my universal apps. There’s no need to use RootViewController.h and RootViewController~iPad.h and write the same code twice! Here is how I use 1 controller for both iPad and iPhone specific code.

If you haven’t read my post on how I use Macros, check it out.

One way to check what device you are running code on is to use UI_USER_INTERFACE_IDIOM(). The UI_USER_INTERFACE_IDIOM returns the interface idiom supported by the current device and is available in iOS 3.2 +.

#define IDIOM    UI_USER_INTERFACE_IDIOM()
#define IPAD     UIUserInterfaceIdiomPad
#define IPHONE   UIUserInterfaceIdiomPhone

Example of using the interface idiom to determine what code to run:
This code says that if the device is an iPad, then it can autorotate, otherwise, if the device is an iPhone or equivalent, the only orientation supported will be Portrait.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    if ( IDIOM == IPAD ) {
        return YES;
    }else{
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    return YES;
}

Another example on when to use this:
Set a specific size for a button, or any other object that has a frame property.

CGRect frame;
if ( IDIOM == IPAD ) {
    frame = CGRectMake(tableView.frame.size.width-105,5,30,30);
} else {
    frame = CGRectMake(tableView.frame.size.width-60,5,30,30);
}
    
UIButton *aButton = [[UIButton alloc] initWithFrame: frame ];

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© Copyright 2011 Wright's Computer Services.   All rights reserved Suffusion theme by Sayontan Sinha