Trouble with loading a separate XIB for iPad or iPhone - iphone

Im having trouble figuring out how to load a separate XIB for iPad or for iPhone in a Universal app.
Its easy enough to convert the Xcode project to Universal and have separate Main view interfaces.
My problem is the secondary view and getting it to have separate interfaces for both iPad and iPhone.
Please offer any help you can give as I have been working on this issue for days without success. Thanks in advance!

I know that #indragie's answer is a very common one, but it's a common misunderstanding that will cause you a lot more work than you actually need to do.
As long as you name the xib files a certain way, they will be automatically selected for either iPhone or iPad. Check out my answer to this same problem on another post:
iOS: Using device modifiers for loading xib files?

Just add a file named MainView~ipad.xib makes it load this one i.s.o. MainView.xib when running on a iPad..

You can use UI_USER_INTERFACE_IDIOM() to check whether the application is running on an iPad or an iPhone/iPod touch:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// Running on iPad
} else {
// Running on iPhone or iPod touch
}
For more information on loading NIB files, read Apple's Resource Programming Guide on Nib Files. You may want to specifically check out the Loading Nib Files Programmatically section which shows how to programatiically load a NIB from within your code using NSBundle. You can then use this in conjunction with the above code to correctly load the proper NIB depending on which device you're runining on.

Related

xcode not creating different app Delegate for universal app

i am totally new to iphone and i am trying to create a universal app.
Now I am creating an empty application. According to all tutorials , by checking universal option it should auto create appdelegates for both iphone and ipad.
But all i can see is only one appdelegate . Kindly tell me how can i create both.
Best Regards
Brayden is correct in answering that you almost never need multiple app delegates. All the delegate usually does is handle the moments when the application launches, suspends or terminates. Back in the days when iPhones ran iOS 4.0, and iPads ran iOS 3.2, you might need very different code in the delegate because only iOS 4.0 supported multitasking. Those days are long gone, and your delegate should probably act the same on all devices.
Yes, you sometimes do reach a point where your program must behave differently on iPhone and iPad. Check the idiom at that time and no earlier. Otherwise you're just duplicating code to no purpose.
My most recent app contains almost no special checks for iPhone or iPad. It doesn't even use different XIBs. Instead, my custom views implement layoutSubviews to fill the space available.
That said, once you understand app delegates, maybe you will find a situation where you need them to be different. If you are absolutely certain that your iPhone and iPad behavior will be so wildly divergent, you will need to:
Manually create a new class (preferably inheriting from the existing AppDelegate class)
In your main.m, send the class name of your new delegate to UIApplicationMain depending on the idiom.
See this answer to "Can I create App Delegate file in my project?" to see the changes to main.m.
You really should only be using one AppDelegate for a Universal application. You can use this to share common things that you'll do in there. What exactly do you need multiple AppDelegates for? If you need to do something specific to a device type (i.e. - iPhone or iPad) then you can do a ternary expression like below:
(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? NSLog(#"iPad") : NSLog(#"iPhone");

iOS Universal App question

I have submitted my iPad app to apple and got approved. Now, i want to add iPhone support to the App.
My question no.1 is:
Q1. is it possible to make the app universal at this moment after submission?
If yes, i have question no.2
Q2. my iPhone app is exactly the same as the the iPad but only a few views are in different look due to the customization in screen size. What should I do in XCode to specify which class that iPhone/iPad is using respectively? I can build them smoothly when i separated them into 2 projects.
Thank you.
You can modify the same application and add support of other device. you need to resubmit application again.
From with in your code you can detect on which type of device it is executing and based on that you can load appropriate XiB file for each view controller.
Yes.It's easy. you have to create Universal app
File--->newproject-->Windowsbasedapplication
and then you select Product type Universal You have a separate view for iphone as well as
ipad
You can identify device using this
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad stuff
}
else {
// iPhone/iPod stuff
}
Most of the UI stuff can be redone just with autosizing parameters, but some don't.
Also for specific classes that do not exist in iPhone/iPod (like UISplitViewController) you can use NSClassFromString, which will return the object class or nil if cannot be loaded.
Q1: Yes, just convert your project to universal and submit it with the same id.
Q2: Fairly easy, conver your project into universal by following the steps here (http://useyourloaf.com/blog/2010/4/7/converting-to-a-universal-app-part-i.html) . The basic idea is to extract your business logic from controllers and use different controller for iPhone and iPad. Don't try to implement it with code block like that
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// ipad goes here
}
else
{
// other
}
note:
If you google it, you will find this link, don't use it, it is outdated -- http://iphonedevelopment.blogspot.com/2010/04/converting-iphone-apps-to-universal.html

Load an iPad nib from an iPhone class

I have a universal app.
I wrote all the iPhone code. Noe, rather than rewrite the code exactly the same, I would like to just have specialized xib files. How do I set up this new xib files. How to I create them and how do I link them to my iPhone classes. then how do I load them, meaning make the app pick the iphone xib or the ipad xib
thanks in advace
Note, I'm using Xcode 3
when you instantiate you controller using alloc init , Use initWithNibName and use the nib or .xib name you to associate with project.
hmm, you say "I have a universal app", but sounds like you mean "I have an iPhone app and want to create a Universal App". If so, here is some info I used the first time I did this:
http://iphonedevelopment.blogspot.com/2010/04/converting-iphone-apps-to-universal.html
Jeff Lamarches stuff is pretty reliable I think.

Building app for iPhone and iPad

Can any one suggest how I can build Universal app for iPad as well iPhone. What all things I should take care of ? How to take care of resource images used ? Is it possible to have same code base should work for iPad as well iPhone.
In the Target->Project->getInfo->Build->target family-> select iPhone/iPad
And make the conditions everywhere whereever you set frame and also the resolution of the image required by iPAD is high.. so as per condition check whether its running on iPad or iPhone and based on that set your frame and image.
hAPPY cODING...
After creating your universal app (see #Suriya's post above) you should figure out in the app delegate whether you have an iPad or iPhone. Here is a simple tutorial to do just that.
Yes, you will need separate nibs and images for an iPad app. But, no, not all the code has to change. You can simply use class inheritance.
Example:
You have a MyTableViewController.h and .m file that work on the iPhone. This table has a custom cell, MyTableViewCell. Now you want your iPad app to get the same information, but display a larger table and a larger table cell. You then subclass your iPhone classes like so: MyiPadTableViewController : MyTableViewController and MyiPadTableViewCell : MyTableViewCell . This way you have access to all of your created functions in the parent class, but you can override how the information is displayed.
If you have a function - (void)doSomething:(id)foo; in your MyTableViewController class, you can use it in your MyiPadTableViewController class without writing any extra code, or override it if necessary. The point is you don't have to change code in two places, so it makes life a lot easier.

Make iPad app a Universal app

I have an iPad app that I would like to make Universal, however this seems alarmingly difficult. I've changed things around so that I support both builds, but when building, I get lots of errors about using UIPopOvers. Here're my questions:
Why does UI_USER_INTERFACE_IDIOM() not compile or register on 3.1.3?
Can I conditionally have variables in a class definition based on UI_USER_INTERFACE_IDIOM()?
If not, should I make two different view controllers?
Thanks!
Why does UI_USER_INTERFACE_IDIOM() not
compile or register on 3.1.3?
The UI_USER_INTERFACE_IDIOM macro is only available starting with 3.2. Same restrictions for the userInterfaceIdiom property of UIDevice. This means that you can only get universal application starting with SDK 3.2.
Can I conditionally have variables in
a class definition based on
UI_USER_INTERFACE_IDIOM()?
No. The UI_USER_INTERFACE_IDIOM macro is only a runtime shortcut to get the current UI idiom of the device.
If not, should I make two different
view controllers?
If you have very different UI between the two devices, it is wiser to use two different view controllers, and to create the right one at runtime (in the application controller for example) by using the UI_USER_INTERFACE_IDIOM macro.
I had pretty good luck with just selecting the target then going to Project->Upgrade current target for iPad.
My app is pretty simple, consisting mostly of table views and webviews, but it's worth making a backup and trying...
And yes, you have to submit it as a 3.2 app that can target 3.1.*
There are also a lot of other stuff that you have to do:
Make sure that it can be viewed at any orientation (got rejected the first time for this).
Make sure that you have all the new images created. you have to have 3 or 4 icons, instead of just one like an iPhone app needed. (iPad icon, iPhone icon, small icon for spotlight, etc).
And of course iPad sized screenshots for the store.
A really nice way that I use to test if it's an iPad or iPhone is to check if the device can use a split view controller, since for the forseeable future no device with a screen as small as an iPhone will be able to use the split view controller. I just created a function to do this for me:
+(BOOL)isIpad{ return NSClassFromString(#"UISplitViewController") != nil; }
Then anywhere in my app that I want to display something different depending on device, I just do a check for it:
int width = 150;
if([NabAppDelegate isIpad]){
width = 350;
} else {
width = 150;
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5,10,width,25)];
This is a lot better than checking against OS version like I've seen some suggest.