Make Better Things



I like to make better things.

Where is MainWindow.xib in Xcode 4.2 ?

In Xcode 4.2 the MainWindow.xib is not included for some of the project templates. Its means now we have to generate GUI elements by code or we can reconstruct the MainWindow.xib in project. I am describing you the second option here.

If you create a new project in XCode 4.2, and choose the Empty Application template to start from, change nothing and try running it in your iPhone 5.0 simulator, you will see an empty – black – screen. The only thing you get from the template is an AppDelegate.h and AppDelegate.m.

We’ll now reconstruct the MainWindow.xib file in our project. So the next thing is now to create a empty user interface file. Choose iOS > User Interface > Empty as template. The name of this file is not very important but we’ll use MainWindow.xib because this name is familiar to us.

Now select the Empty MainWindow.xib file we just created in previous step.

Change the Class of File’s Owner to UIApplication

Now drop a Object from Library to Objects in xib file.

Change the class of that object to the AppDelegate class

sd

Now add a window to objects pane.

Now we need to bind this window object to our code. To do this we have to add IBOutlet to window object in AppDelegate.h file. You code of AppDelegate.h should look like this -
</pre>
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) IBOutlet UIWindow *window;

@end

Note the IBOutlet just before UIWindow.

Now continue with editing MainWindow.xib file -

Control-Drag from the delegate outlet of the File Owner to the xAppDelegate object.

Control-Drag from the window outlet of the xAppDelegate to the Window.

Navigate to the project, and in the Summary tab, select MainWindow as the Main Interface.

We are almost done now..  But there is one more thing we need to fix. In AppDelegate.m, there was actually code that creates a window as well, so we have to delete this code.

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

We are done now

Category: iPhone, xcode

Tagged: , , ,

25 Responses

  1. ciara says:

    Thank you so much for this tutorial! This really helped de-mystify the changes in the generated project files after I upgraded to 4.2.

  2. Joe says:

    This all worked great until the third from last step – “Control-drag from the window outlet of the xAppDelegate to the Window.”
    When I get to this step, there are no outlets listed. Is there something I have to do to make the window outlet known here? I added IBOutlet to the .h file as instructed two steps prior (and saved the file), but that doesn’t seem to have been enough.

    • Saurabh says:

      I think you are missing “Change the class of that object to the AppDelegate class” step.. please make sure you follow this and double check the IBOutlet in .h file

  3. Such_a_N00B says:

    Thank you so much !!!
    You helped me a lot.

    But the question is : “Why – in the name of god – do they have to change basic templates every iOS update?”
    I had not enough time to learn and fully understand iOS 4 dev that iOS 5 is already out and everything has changed, so no way to follow easily the tutorials.

  4. Andrew says:

    I was initially puzzled by this as well. When you change the class of the object you’ve added to MainWindow.xib, you need to change it to whatever your App Delegate class is actually called, not just AppDelegate. So if it’s called MainWindowAppDelegate then you need to change the class of your object to MainWindowAppDelegate as well.

  5. Mark says:

    Can you write how to do MainWindow with tab bar controller?

    Thank you!

  6. Chinese says:

    Thank you very much, this tutorial help me a lot, thanks again.

  7. Bharanidharan says:

    Thank you very much….!!!!!!!!

  8. Mike says:

    thank you so much for this great tutorial!

  9. Tomás says:

    Man! You absolutely save my life! Tks!

  10. Julia says:

    Thank you!!! It is very heplful!

  11. Deepak says:

    I was stuck and you helped me man. Thanks a lot.

  12. My Homepage says:

    … [Trackback]…

    [...] Read More here: makebetterthings.com/iphone/where-is-mainwindow-xib-in-xcode-4-2/ [...]…

  13. Dave says:

    Thank you so much pal!

  14. Ani says:

    Thank you so much.

  15. K3nobi says:

    Thank you so much! I’ve been learning xCode with the help of old tutorials so your explanation of these changes helped a lot.

  16. GT says:

    Another “thank you” for this article. I’m picking up some sample code from Dropbox and this explains what was missing when I tried to recreate it in Xcode 4.2.

    Thanks!

  17. Jeffrey says:

    hello,

    With the last step my code looks a little different. It looks like this…

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    //self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }

    I just commented it out because I didn’t want to delete anything.

    Will this work?

    Jeff

  18. malik says:

    Awesome !!! It helped me learn concept of what is going on behind the scene …

  19. Rishi says:

    Thanks man. This helps me a lot.

  20. Deny says:

    Good Job Man …Great Stuff

  21. Suresh says:

    I(t always crashes with Following message

    *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘There can only be one UIApplication instance.’

    Have you missed some changes to be done in main.m file

  22. Kyle says:

    Thank you so much! I was doing all of this already except the last bit where you had to delete the line of code that creates new window in the .m file. I was starting to get really frustrated. Again, thanks a lot!

Leave a Reply