5StringRyan
5StringRyan

Reputation: 3634

How to hookup a MainWindow.xib to an empty project

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/

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.

Upvotes: 3

Views: 2274

Answers (2)

Jon Hess
Jon Hess

Reputation: 14247

  • 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.

Upvotes: 4

user1163722
user1163722

Reputation:

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

Upvotes: 0

Related Questions