Load an iPad nib from an iPhone class - iphone

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.

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");

How to eliminate Storyboard

I have an iPhone app in the store that I used xCode 4.2 and Storyboards to create. I now wish for iOS 4.3, and prior, users to be able to use the app... they currently cannot due to the storyboard.
How do I proceed to eliminate the storyboard to gain a wider audience?
Thanks.
There is no easy way to do this, unfortunately. What I would suggest is to manually copy the elements of each segue in your storyboard and paste them each one at a time to a newly created nib file...
Also, dont forget if you go the multiple nibs way, you'll need to edit a bit your AppDelegate and create a MainWindow.nib file. Create a new project using nib files to have a perfect exemple of what to do!

What is the difference between a .xib file and a .storyboard?

Can someone explain in simple words the difference between .xib and .storyboard?
Apple introduced the concept of "storyboarding" in iOS5 SDK to simplify and better manage screens in your app. You can still use the .xib way of development.
Pre-storyboard, each UIViewController had an associated .xib with it. Storyboard achieves two things:
.storyboard is essentially one single file for all your screens in the app and it shows the flow of the screens. You can add segues/transitions between screens, this way. So, this minimizes the boilerplate code required to manage multiple screens.
Minimizes the overall number of files in an app.
You can avoid using Storyboard while creating a new project by leaving the "Use Storyboard" option unchecked.
You could refer this tutorial to get started.
Yes, you can still create a Window-based application for iOS 5. If you use the "empty project" template, you will see that a window is created for you in the app delegate. From there you can add XIB files as normal, or a new storyboard.
I'm assuming you mean "storyboards" rather than "timeline". Storyboards allow you to map out, visually, all of the views in your applications and how they interrelate. If you are just starting out with storyboards, there's an introduction to storyboards in the WWDC 2011 videos here. The 2011 Stanford iOS course on iTunes-U is also iOS 5-specific and covers storyboards and more.
A storyboard is like a canvas where you put all your .xib files. You no longer have any .xibs, you just have View Controllers directly on your canvas.
storyboard is a new feature available since the release of Xcode 4.2.
It offers a complete new way for iOS developer to create and design
user interface. Before the introduction of Storyboard, it’s especially
hard for beginner to create navigation (and tab) interface. Every
interface is stored in a separate xib file. On top of it, you have to
write code to link all interfaces together and describe how the
navigation works.
With Storyboards, all screens are stored in a single file. This gives
you a conceptual overview of the visual representation for the app and
shows you how the screens are connected. Xcode provides a built-in
editor to layout the Storyboards. You can define the transition (known
as segues) between various screens simply using point and click. This
doesn’t mean you do not need to write code for the user interface. But
Storyboards significantly reduce the amount of code you need to write.
Source: http://www.appcoda.com/use-storyboards-to-build-navigation-controller-and-table-view/
XIB:
Xib files are used with a single UIView.
2)It's very difficult to implement complex auto-layouts in xib.
3)It's utilizes more memory as compared to storyboard and quiet slow.
It is compatible from iOS5 and onwards
You can do localizations for different languages and countries using
different XIBs .
It's difficult to use same Xib to support multiple devices.
Storyboard
1)You can layout all your Scenes like View Controllers, Nav Controllers, TabBar Controllers, etc in a single storyboard.
2)You can use Auto Layout easily that defines mathematical relationships between elements defining their position and sizing.
3)Usually fast and allocates less memory.
4)It's not compatible prior to iOS 5 .
5)"Dynamic" and "Prototype" cells can be used easily.
6)Storyboards best to use for the apps with a small to medium amount of screens.
The best Answer I have seen : Xib Vs Storyboard in iOS
XIB and Storyboard are used for creating interfaces for users.
One important point is,xibs are used for creating a single view(it has single file owner at the top of the xib file), but in-case for viewcontroller, multiple screens can be added and its flow can also be monitored(it has separate file owners).

Trouble with loading a separate XIB for iPad or 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.

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.