How would I change the default loaded view controller in Xcode? - iphone

How would I do this? Is it simple enough to explain clearly? If not, a tutorial would be nice. I checked the web and this site and still couldn't find exactly what I was looking for.

The default XIB file loaded when your application start is MainWindow.xib by default.
If you want you app to load another XIB instead, this can be changed in the Info.plist file of your project.
In this XIB loaded when the app is launched (MainWindow.XIB by default), you will find:
a placeholder for the File's Owner (like in any XIB) which in the case of the XIB loaded by the application on startup is the UIApplication itself.
a UIWindow (the main and unique window of your iPhone app),
an object that acts as the delegate of your UIApplication (commonly called "the AppDelegate")
And probably a UIViewController too.
When the XIB is loaded at startup, the AppDelegate objet is instanciated (like all objects in the XIB except the File's Owner) and as it is set as the delegate of the application, application:didFinishLaunhcingWithOptions: will be executed. This code then generally add the viewController's view as a subview of your app window using a line like [self.window addSubview:self.viewController.view]. (As your AppDelegate have an IBOutlet that points to the ViewController in the XIB)
If you need to change the class of the ViewController used in your MainWindow.xib, change the class of the UIViewController in Interface Builder, and also change the type of the associated IBOutlet in the AppDelegate header file.

Related

Where can I change the window my app uses from UIWindow to my own subclass "MyWindow" with storyboard?

I've got a universal project here with storyboard. I've created a subclass of UIWindow called MyWindow, and I need to load it instead of the default UIWindow. Prior to storyboard, I would simply go to the .XIB file in XCode and change the class for the main window to MyWindow. However, I'm unable to find any section where I can change this in storyboard.
Does anyone know where I can do this? I need the main window to load MyWindow, not UIWindow.
When using storyboards the application delegate and main window are no longer represented in Interface Builder. Instead, when your app starts, your app delegate is queried for a property called 'window'. If this returns nil then a default window of type UIWindow is created for you. To change this behaviour you need to implement a 'window' getter method in your app delegate that returns your 'MyWindow' subclass. See the documentation.

MainWindow .xib - iPhone Development

It is my understanding that every view has it's own controller class. I know generally that the xib file is the application view/subview. My question is does the MainWindow.xib have it's own controller and if so, where can it be found?
You are right in that a xib file can allow to associate a view to its view controller.
Now, MainWindow.xib contains the main UIWindow for your app (at least). UIWindow is not a UIView and does not need a UIViewController.
On the other hand, you can create any object you like inside of a MainWindow.xib, so you can also have a UIView in there, which you then add to the UIWindow instance, and its corresponding UIViewController.
If you think about additional xibs, what happens with them is that they define a UIView, and additionally also specify the File's Owner type, which is usually UIViewController and gets instantiated by loading the xib.
In this sense, MainWindow.xib, though not requiring a UIViewController, still needs a File's Owner, and this is the UIApplication singleton. Since you cannot modify nor derive a class from UIApplication, the way to interact with the UIWindow instance is through the UIApplication's delegate.
Take in mind that MainWindow.xib plays a special role, in that it is also specified in the info.plist file. You can do without one (by removing the corresponding entry from info.plist) and simply declare your application delegate when calling UIApplicationMain from main.c. In this case, nor application delegate neither the UIWindow will be instantiated through the xib mechanism; you will need to instantiate a UIWindow from you application delegate's applicationDidFinishLaunching.
I don't think it has its own controller. It is the main window basically the thing that pops up when application comes up. We add controllers or view controllers for the user to use. App delegate is sort of the controller but not exactly. we override applicationDidFinishLaunchingWithOptions to any controllers etc so appdelegate can be thought of as the controller of the mainwindow.xib although it isnt.

Why must I hookup an IBOutlet twice in a custom NIB / XIB?

I have created a custom myViewController class and it has the default view, as well as an IBOutlet (topleftView) to a subview.
I have created a custom NIB/XIB file to load this myViewController.xib. Inside the XIB file I have set the file owner to myViewController and set the UIViewController identity class to myViewController as well.
My question is why do I have to hook up and draw a reference from the IBOutlet in the subview to both the file owner AND the UIViewController in interface builder?
Just trying to get my head around it since this is the first time I'm creating a custom NIB/XIB. I usually just did everything in MainWindow but my application is getting too large so I want to spread things out.
If I don't have these multiple connections for the one IBOutlet to both the UIViewController and File Owner in the same nib file I crash with _EXC_BAD_ACCESS_ errors.
"Inside the XIB file I have set the
file owner to myViewController ..."
I'm not sure what you are doing here. I think this is where the problem is. How many items do you have on the top level of your XIB? It should just be File's Owner, First Responser and a View. If there is another controller object in here, that's your problem. Get rid of it.
"...and set
the UIViewController identity class to
myViewController as well"
This part is correct. To connect your custom UIViewController to the XIB, clock on your "File's Owner", go to the "Identity Inspector" then look under "Class Identity" at the "Class" field. Set this to 'myViewController' (or whatever you named it).
At this point you should do 1 ctrl-drag from your File's Owner for each outlet you have setup.
My question is why do I have to hook
up and draw a reference from the
IBOutlet in the subview to both the
file owner AND the UIViewController in
interface builder?
Short answer: You don't. Just set your File's Owner's class to the class name of your UIViewController subclass and you're set.

How to provide an own UIWindow subclass as the main window in an iPhone app?

For inspecting all events posted to the views of the window, I want to make a subclass of UIWindow and make that -keyAndVisible in the app delegate.
However, my project already came with an nasty MainWindow.xib file ;-) so I find it hard to do that now, since that nib guy is creating the window. Is there any way I can get rid of this MainWindow.xib? Or is there another way to have an subclass of UIWindow in place?
Or in Interface Builder you can change the Window's class from UIWindow to your subclass in MainWindow.xib (using the Identity Inspector).
Just delete MainWindow.xib remove refrences from info.plist and your app delegate. Then you can create an instance of your custom window in the app delegate and call keyAndVisible like the app delegate currently does.

Very basic problem with Tab Bar Application and Interface Builder

OK, here is how to re-create the problem had:
Create a new project, using the
Tab Bar Application
Add a UILabel within SecondView.xib
Add IBOutlet UILabel* myLabel; to FirstViewController.h
Connect up myLabel in IB.
Build and run.
When I click the second tab the app crashes with:
__TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__
In IB, when using UITabBarController which has multiple UIViewControllers which all separately have their own NIB file assigned, there are two places where you need to set your UIViewController class file.
First, the obvious one was within the NIB file for each UIViewController.
Secondly, where I'd missed it, is withing the NIB file for your UITabBarController. In each UIViewController, not only do you set your NIB file, but also the Class.