find xib for iPad - iphone

I load my xib files through: initWithNibName programatically. If I set something like: [[LoginController alloc] initWithNibName:#"LoginController" bundle:nil];
Shouldn't it load LoginController.xib if it is executed on iPhone and search for LoginController-iPad.xib on the iPad? This is not happening at my code, do I have to select the right one manually or is something going wrong?
Thanks in advance!

No, there is nothing in the documentation that suggests "-iPad" is a valid resource-like modifier for initWithNibName:, or that initWithNibName: supports modifiers at all.
BTW, .xib files are compiled to .nib files by Xcode, so the application never sees a .xib.

This is an old question, so perhaps this naming convention did not exist when #shaggy frog answered this question, but iOS should indeed load the iPad specific xib if it is named correctly. In this case your naming is slightly off, the correct naming scheme for an iPad specific xib is
NameOfYourViewController~ipad.xib
Note that it's a tilde (~) instead of a dash (-) and ipad is in lowercase.

Related

integrating iOS4 project into iOS5 project

I have a newbie question about integrating two iOS apps. I have created an app in iOS 5 (my first app so I dont have any knowledge of iOS 4 except the fact thah there were xibs files instead of storyboard and also ARC was not included).
Now I have to include one older standalone app (built for iOS 4 with xibs and non ARC) to my iOS 5 app. Lets say that in my app on Main menu view there will be a new button opening the main menu of the other app.
So I did some research and find out how to disable ARC by the fno-objc-arc flag. Fine, now I have imported all the files of the second app to mine app and all the classes have the flag set.
I can still run my app without problem.
Now I have no idea how to let my new button to open the mainViewController of the old app - this app has MainWindow.xib (contains a window and one navigation controller). This MainWindow is set to be Main Interface in the old project. There are also some init call in appMainDelegate file. Where can I call them in my app?
Could anybody tell me what needs to be done. I have an idea, that I will add only one new UIViewController to my storyboard. This will be the starting point for the old app and than everything will work as it used to. Or will I have to create more controllers (for every xib) in my storyboard? This is where I dont know what to do. Any help much appriciated. thank you
Try doing something like the following, anything like it or simmular should work:
In the method of the button (the one you call your new button) have it execute the following code:
OldAppMainViewController *controller = [[OldAppMainViewController alloc] init];
//Here you can assign vallues to any properties that you might want to
[self.navigationController pushViewController:controller animated:YES];
That old apps main view controller should have a init with nub named function so it should work. If this does not work try something along the lines of:
OldAppMainViewController *controller = [[OldAppMainViewController alloc] initWithNibName:#"OldAppMainViewController" bundle:nil];
Where the NIB name is the .xib file name without the extension.
Also make sure that all connections in interface builder is setup correctly

Updating iPhone app to Universal: IBOutlets

First off, I will say i've spent 6 hours on this topic and have read everything the internet has to provide, which is why i came here.
I have converted to Universal, Xcode created the MainWindow-iPad.xib and everything seems fine.
Here are my questions:
1) What are the naming conventions for new iPad-specific xibs? Xcode created "-iPad" but i believe im supposed to be making them "~ipad". Why the difference?
2) (MOST IMPORTANT) After creating several "~ipad" xibs, Xcode seems to know to load these. So I'll copy the content in say, "RootViewController.xib"
and paste it in "RootViewController~ipad.xib". THIS IS THE PROBLEM: this new ~ipad xib has no outlets or referencing outlets!
I can't link the buttons on my page to anything. How do i do this without having a separate ~ipad .m and .h for everything?
Thank you guys for your help! I'm going to write a tutorial on this once I get this all working.
Just set the class of that ~iPad nib to be the same classname as the cooresponding iPhone nib. This is done in the inspector in Interface Builder. You may have to connect the outlets back up depending on the order you do things. I would think that if you copy the objects from the iPhone nib to the iPad nib AFTER you set the class, then the outlets would stay wired up.

iPhone Obj C - change startup root class?

I am learning iPhone Obj C slowly. I have a XIB with several views and all works well. I need to have a 2nd XIB to control another set of views but haven't been able to make it work.
So I created the 2nd class and a 2nd XIB, all called one.h one.m one.xib and the same for the new one is all two.*
As it didnt work I was going try and change the app to start on the TWO class rather than the ONE class. In the plist I changed the Main Nib base file but that didnt seem to do anything.
Where do you specify what the start up class is? That way I can make sure I did everything correctly first, and then go back to the code that is supposed to call the two class and xib.
Also if anyone has any sample code to go from one class and xib to another, please let me know.
thanks!
In your AppDelegate class make sure the ViewController being allocated and set to the window is the one desired.
Your project's [ProjectName]-Info.plist file decides which Nib file is used when the application starts, in the key NSMainNibFile. (By default, this is set to MainWindow.)
A standard MainWindow.nib file will define the "root" class, which is usually [ProjectName]AppDelegate. You can, however, change this by editing the nib.
I had to set the CLASS IDENTITY to the proper class for the app delegate on the XIB for both XIB.
Then I changed the plist to point to the XIB I wanted to start as root.
Both of the answers above helped me find this.
THANK YOU

Xcode 4: nib files not loading when run

So, I just downloaded Xcode 4 and installed it. I was actually quite looking forward to the single window and integrated IB...
-
However, when I open and run one of my projects, the nib files that the project uses don't seem to load. Instead I'm left looking at a blank white screen (iPhone). This project ran well and fine on Xcode 3.2.
If I background and un-background the app, the view loads fine. But this happens every time I build, on both iPhone and iOS simulator, i.e. the app doesn't work properly until it's been backgrounded. All the code for loading the view follows from
- (void)applicationDidFinishLaunching:(UIApplication *)application.
-
Did anyone else have the same nib file problems - is there a fix (or something stupidly simple that I'm forgetting about)?
Ok after a lot of messing around, I figured out the nib problem.
I had a stray line of code in the appDelegate class:
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
This line of code doesn't affect anything when compiling with Xcode 3.2, but with Xcode 4 causes the loading problem. Just remove it (I don't even remember why it was there...) and it's fixed :)
I'm not sure what's going on with your nibs, but this XCode 4 transition guide should be of some help for finding missing panels, menus etc...
I had the same problem. In my case, for some reason Xcode had decided to hide the view in question. It cleared up when I put the following into the viewDidAppear method of the view controller:
self.view.hidden = NO;
For anyone with the same error. If upper won't help - check if you did not overwrite the view attribute of your ViewController.

iOS: Using device modifiers for loading xib files?

You can use a device modifier (i.e., ~ipad) to provide a device-specific key in Info.plist, and to specify a device-specific launch image (Default.png for iPhone, and Default~ipad.png for iPad, for example). Those two things are specifically mentioned in Apple Docs, but they don't say that this will work for any other kinds of files.
I've discovered (quite by accident) that this works for loading .xib files via initWithNibName:bundle:. So for example, I can have MyView.xib and MyView~ipad.xib, and this code:
MyViewController *viewController = [[MyViewController alloc]
initWithNibName:#"MyView" bundle:nil];
... will totally load MyView~ipad.xib on an iPad, and MyView.xib on other devices.
So, 1) Is this documented somewhere? I sure couldn't find it any any Apple docs. It's sure handier than checking UI_USER_INTERFACE_IDIOM() and hardcoding two different nib names everywhere, but I kinda don't trust it if it isn't documented.
And, 2) Does anyone know what version of iOS this started working in? I've only tried it in 4.2, and it works there. Device modifiers in general (even for the documented things listed above) are 4.0 minimum.
I had this same problem. The answer didn't make sense at first, but the good news is that it's easy to do! :)
Just name your iPad xibs without any modifier and your iPhone xibs with ~iphone modifier and it'll select them correctly.
So, with MyViewController, you'll have MyViewController.xib for the iPad and MyViewController~iphone.xib for the iPhone. Then you can just init your view controller with simple alloc/init.
[[MyViewController alloc] init] and it'll grab the right xib.
So, when I create a new view controller in XCode, I always choose the box to format it for ipad, because the xib it will create will be named MyViewController.xib and you want that one to be the iPad sized xib. Then I create a second xib, formatted for iPhone and name it with the ~iphone modifier.
The documentation is a little contradictory at times, but this page talks about how resources with an identifier will default to iPad.
ImageSoundResources
Check the section about using high res images. I know we're talking xibs and not images, but it does work. My last 6 apps have all used this idiom.
Actually, it is explicitly defined in the docs, but as a footnote.
CocoaNibs
In the note at the bottom of "Loading NIB files using NSBundle":
Note: If you are developing a
Universal application for iOS, you can
use the device-specific naming
conventions to load the correct nib
file for the underlying device
automatically. For more information
about how to name your nib files, see
“iOS Supports Device-Specific
Resources.”
Which links to Cocoa Conceptual LoadingResources
However, yes, this is a 4.0+ only feature.
I hate to be that guy and answer my own question, but I think the answer is:
1) Nope, not explicitly documented in any Apple documentation, and
2) 4.0 and higher (this based on my own testing)
All you really save is a couple lines of code checking for UI_USER_INTERACE_IDIOM(). Still, I'll take it. Less code is less code.
The appropriate technique to use in iOS 3.2 and later is the UI_USER_INTERFACE_IDIOM() function. I typically use a ternary operator to init the UIViewController with the appropriate XIB.
UIViewController* controller = [[UIViewController alloc]
initWithNibName:UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ?
#"YourViewController-iPad" : #"YourViewController" andBundle:nil];