3

Beginning with XCode 4.2, when you create an empty project using XCode 4.2, a MainWindow.xib is no longer created and hooked up for you. I've found an article that describes how to do this and I've done it and it works, but if this process has taught me anything, it has shown me that I have no idea how main(), AppDelegate and the MainWindow.xib exist together.

http://www.trappers.tk/site/2011/06/16/mainwindow-xib/

  • Why is MainWindow.xib class updated to UIApplication?
  • Why is an object placed on the xib, and then AppDelegate class is selected for the class?
  • Why is the delegate outlet of the File Owner connected the AppDelegate object?
  • Why is the the window outlet of the AppDelegate to the Window?
  • why does - (BOOL) application:didFinishLaunchingWithOptions: need to be commented out.

I've researched around but, I'm still not 100% sure how everything is loaded up once the application starts, and why this is setup is needed just to have a MainWindow. I have a feeling though that I should probably get all these concepts down to continue to advance in iOS development.

2 Answers 2

4
  • Why is MainWindow.xib class updated to UIApplication?

At the lowest level, nib files are loaded with the method -[UINib instantiateWithOwner:options:]. The File's Owner in a nib file is a placeholder. In Xcode, it isn't a specific object yet. It will resolve to an actual object when the nib file is loaded. Its purpose is to relate, via outlets and actions, objects inside of the nib with the object that loaded the nib. The object passed as the "instantiateWithOwner:" parameter of that UINib method is what the File's Owner placeholder in Interface Builder will resolve to.

UIApplication loads the nib file specified in the info.plist and passes 'self' for the owner parameter when loading the nib file.

By setting the class name, you're just hinting to the tools so that they can suggest the set of actions and outlets you're allowed to establish.

  • Why is an object placed on the xib, and then AppDelegate class is selected for the class?

When you place the generic object in the xib and change its class to 'AppDelegate' you're telling Xcode to instantiate an instance of 'AppDelegate' when the file is loaded.

  • Why is the delegate outlet of the File Owner connected the AppDelegate object?

UIApplication has a 'delegate' that it delegates responsibility to and notifies when interesting events occur. When you make this connection, you're setting the delegate property of the application to be the instance that you specified above. From that point on, this instance will receive the delegate messages from UIApplication, like -application:didFinishLaunchingWithOptions:.

  • Why is the the window outlet of the AppDelegate to the Window?

Outlets are a way to refer to objects inside of a xib. They cause properties or instance variables to be set to refer to the object pointed to by the outlet. When you make this outlet, you're making it so that the app delegate instance you created above has a way to refer to the window that's also created when the xib is loaded.

  • why does - (BOOL) application:didFinishLaunchingWithOptions: need to be commented out.

It represents the code-focused way to do some of the same things that are happening in the xib, and if they were both present, they would be overwriting each other.

2
  • Awesome stuff! Just a few quick clarifications. Since AppDelegate is primarily used to delegate responsibility to receive "events" from the UIApplication, does this mean that apps will have only one AppDelegate class? Also, is MainWindow.xib the "thing" that instantiates the AppDelegate and any other objects (such as controller classes)? Sep 12, 2011 at 14:49
  • Yep, applications will only have one app delegate class. UIApplication is a singleton, and the 'delegate' property only holds one object. "Also, is MainWindow.xib the "thing" that instantiates the AppDelegate and any other objects (such as controller classes)?" It's a way to do that, which is what it's doing in this project, but there are other ways. For example passing the delegate class name to UIApplicationMain() and instantiating the rest of the objects in application:didFinishLaunchingWithOptions:.
    – Jon Hess
    Sep 13, 2011 at 3:23
0

The object placed on the XIB file is the AppDelegate because it delegates all connections in the Interface Builder, meaning if you write a method that when a button is clicked it displays text, that method would be connected to either the App Delegate or the File's Owner, preferably the delegate. The File's Owner, since it is the UIApplication, connects to the delegate because it assigns that certain object to be the App Delegate. Sorry that I couldn't answer the rest of your questions, they didn't really make sense.

Hope this helps

2
  • Yeah most of the questions are based on the link that I provided that has a step by step instruction of "hooking up" a MainWindow.xib, but annoyingly enough doesn't explain why or how you would make sense of this if you were on your own. Sep 10, 2011 at 2:53
  • I know, it was hard for me at first, but it just takes some practice to really learn what's going on. Good luck!
    – user1163722
    Sep 10, 2011 at 3:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.