tags:

views:

52

answers:

2

Problem: In my iPhone app code, I'm positioning and animating a lot of views programmatically. For the iPad, I want to provide a completely different user interface, but also programmatically. I don't like to use Xib files. How can I make different views and view controllers and load these depending on if it's an iPad or an iPhone? What's the cleanest way?

+2  A: 

You just have to specify the device identifier, you can do that by adding the string "~iphone" or "~ipad". So for a view controller this would be; "MyViewController~iphone.m" and for the iPad "MyViewController~ipad.m"

Jeroen de Leeuw
+1  A: 

In addition, if you are having compilation problems because you are programatically creating classes only available to iOS 3.2 and above you can use code like this:

Class popClass = NSClassFromString(@"UIPopoverController");
if(popClass) {
    id infoPop = [[popClass alloc] initWithContentViewController:popViewController];
    [infoPop presentPopoverFromRect:CGRectMake(20, 70, 10, 10) inView:self.view permittedArrowDirections:4 animated:YES];
}
Ben
You should use `UIUserInterfaceIdiom` instead.
Alexsander Akers
That will detect if the code should be run, but it won't help during compilation if your target is pre-3.2. It could certainly replace the test but NSCLassFromString would still be needed to create a class the target device might not know about.
Ben