2

I'm trying to write an app without using nib, everything I'll do it programmatically.

Now the problem is, how am I going to support both iPad and iPhone? Obviously, I can't do it with

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    // load iPad nib
} else {
    // load iPhone nib
}

If I create 2 ViewControllers, then the IBAction will be redundant.

Any suggestion?

Js Lim
  • 3,625
  • 6
  • 42
  • 80
  • you can set the frame alone in the above `[UIDevice currentDevice]` condition and do the functions commonly..... I think without using this you can't do..... – Venk Mar 14 '13 at 05:16
  • 1
    If everything is programmatic then why you want to create two view controller?? one is enough – Anil Varghese Mar 14 '13 at 05:16
  • Have you selected universal application when you were creating the project. – Dipendra Mar 14 '13 at 05:24
  • check my answer at http://stackoverflow.com/questions/13139430/objective-c-how-detect-iphone-iphone5-and-ipad/13139561#13139561 – Nitin Gohel Mar 14 '13 at 05:30

4 Answers4

3

You should probably just figure out the device type in applicationDidFinishLaunching and then load separate controllers for each device. But if you want to just have a single implementation for all devices, do checks like this:

 if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
    //do some iPad stuff
}
else
{
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;

    if([UIScreen mainScreen].scale == 2.f && screenH == 568.0f)
    {
        //do iPhone 5 stuff
    }
    else
    {
        //do iPhone 4S and iPhone 4 stuff

        //the dimensions are the same however, if you want to do iPhone 4S specific stuff
        // you'll need to do additional checks against video resolution or other differences etc
    }
}      
Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
0
CGFloat height = [UIscreen mainScreen].bounds.size.height;

if(height==568.00 || height == 480.0)
{
//For iphone 5 and iphone 4

}
else
{
 //For ipad
}
Rushabh
  • 3,208
  • 5
  • 28
  • 51
0

You can use this code in your AppDelegate

- (BOOL) isPad() 
{   

    if(UI_USER_INTERFACE_IDIOM)
    {  
        return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
    }
   else
   {
        return NO;
   }

}

This link gives some info about the idiom http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/macro/UI_USER_INTERFACE_IDIOM

OR

You can check the height and width of the screen to know whether its an iPhone or iPad

CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
Sharanya K M
  • 1,805
  • 4
  • 23
  • 44
0

If you are not using any nib, everything doen programmatically you dont want to create two view controllers for the iphone and ipad. Remember do not depends on any static values. ie your calculations should be made according to self.view.bounds some thing like that (I mean to create views/subviews). Then if some specific functionality that supports only in iPad do checks

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)

One view controller done all the job for you.

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • I can do that if the look for iPad is same ask iPhone, but what if iPad has a sidebar? then there are different views – Js Lim Mar 14 '13 at 06:05
  • In that case its better to create two view controllers. Present the view controller according to the device. Or you can load different views in the `loadView` method of the view controller accordingly. Anyway you have to use delegates to handle actions – Anil Varghese Mar 14 '13 at 06:15
  • you mean create 2 views for 1 ViewController? but in this case I have to implement 2 delegate right? – Js Lim Mar 14 '13 at 07:27
  • No. Your single view controller becomes the delegate of both views. Prefer this implementation if you have more redundant codes(functionalities are almost same in iphone/ipad). The changes in view does not cause any error if handle it properly. You can make sure that your buttons have single targets(both views have same delegate) – Anil Varghese Mar 14 '13 at 07:37
  • [Check this](http://stackoverflow.com/questions/15361391/trigger-a-method-in-uiviewcontroller-from-its-view/15363649#15363649) – Anil Varghese Mar 14 '13 at 07:37
  • Let say now I have `IpadView` and `IphoneView`, so in my `viewController` should it be `@interface CKMainViewController : UIViewController ` ? – Js Lim Mar 14 '13 at 10:41
  • Ya.. thats better. or you can define all the delegate methods in a separate file under a single protocol... That up to you... – Anil Varghese Mar 14 '13 at 11:05
  • what if subclass it? `IpadViewController : IphoneViewController`, does it have any drawback? – Js Lim Mar 15 '13 at 02:05