XIB files are important in working in Cocoa and macOS development. This video shows how the interface in Interface Builder is set up, how to view the code behind your XIB files, and other features in working with XIBs.
- [Instructor] Let's go into a little bit more detail about the Zip file, and Interface Builder. As you've already looked at, the Zip file contains our user interface objects. But, what we haven't looked at is that actually behind the scenes, the Zip file is just an .XML file. You can see that by right clicking it, and choosing Open As, Source Code. And there's the code there. With all the information about all the assets used in your Zip file, and where they're located.
You can go back to viewing it in the standard way by right clicking, and selecting Open As, Interface Builder Zip Document. At run time, the data in the Zip file is actually used to create all the user interface objects that you put inside your layout. In Interface Builder we've looked at the basics of using objects by dragging them out of the library. And we've looked at some basic settings using the Attributes Inspector, for example.
But, I want to go into a little bit more detail about how the Zip file is organized, and how you can access different parts of it through Interface Builder. At the bottom left corner of Interface Builder, you're going to see a button that says Show Document Outline when you hover over it. The Document Outline gives you this hierarchy for viewing your application. And it has three sections. Placeholders section, Objects section, and a Windows section. So far we've mostly used the Windows section. Window of course contains the main window for your application, and all of its sub-views.
So when we drag out a push button, for example, and we drop it in the window, we see that it gets added as a sub-view of this main view. So the Window is just a hierarchy of views. And we can expand and collapse those by using the arrows on the left side of each icon. The Objects section contains objects that are part of your application, but are not in the user interface. They're not inside of the window. This includes your app delegate object.
You've already looked at this so far, it references AppDelegate.swift. And there's your Font Manager, which controls which fonts are accessible by your application. The Main Menu is the menu for your application that goes at the top of the screen. Including the name of your app, File menu, Edit, et cetera. And of course, you can add custom menus if you want, we'll look at doing that later on. Just note for now, that you have this hierarchy on the left side of the Document Outline, and that in the main window, you can actually click on these elements and navigate through them.
You can even see what items are connected to the menu elements by right clicking them. So you can see that About our app has a set action already, so it triggers an action. Again, we'll talk about this more later on. For now, I'll collapse that Main Menu object in the Objects section, and we'll talk about Placeholders. The Placeholders section in the document outline is for those objects that you might want to access with your user interface, but that don't exist until run time.
File's Owner is one of those. File's Owner refers to the object that created the Zip file. Now, if you look at that, and you select it, and you head over to the Identity Inspector in the Inspector bar, you're going to see that this is referring to the NSApplication. So for the main window of your application, this is going to be referring to that NSApplication instance. But if you create other windows, you might have a different File Owner.
So if we create a Preferences Window or something, we can have a different Owner for that. And that object can be referenced here. In First Responder, the First Responder refers to the object that is receiving events currently. The first object in the event hierarchy. Now, you may be wondering why we would need to access this at design time. And the reason for that is sometimes we have certain actions that we're don't want to apply just to one object, we want to apply it to multiple objects.
Now, think of copying and pasting in the menu. For this, the item that we want to copy or paste, is going to be whatever text is currently selected. So, the First Responder goes through this whole hierarchy of the currently selected object and then it goes up the chain all the way up to the application. Now, just to clarify, I'm going to show you the diagram in Apple's documentation. Now, this diagram says the Responder chain in iOS, but it still applies to Mac OS.
So we have this view, our main view, maybe it's a sub-view of another view. And it has an object in it. Maybe it has a text filled in it. And that is the current First Responder. Where your cursor is actively in that text field typing in text. So it's responding to the text events. The first object to respond to those text events. And if there is an event, it could be sent to that First Responder. If the First Responder doesn't have the appropriate method to handle the event, it goes up the chain to that Views, View Controller, and then to the Parent View, and that views View Controller, and then to the Window, and finally to the Application.
And if the application doesn't have a way to handle that event, then the event is discarded. So that's how the First Responder works. Let's go back into Xcode. And I just want to emphasize that if that didn't make a whole lot of sense, just understand that the First Responder is sort of a generic object that's going to reference the currently selected object at the time. Usually you use First Responder with a menu command, like Copy and Paste, or when you're going to make a text object bold.
So it can apply to whatever is currently selected. The Application object in the Placeholders section is simply a reference to your application. And no matter how many windows you may have on your application, there is always one Application instance, and it's always accessible here. Again the same object is accessible in every Zip file you create through this Application object under Placeholders.
- Installing your development tools
- Working with Xcode and the MVC pattern
- Creating basic interactions
- Creating delegate classes
- Building a user interface
- Laying out the UI
- Working with data and table views
- Binding data objects to the UI
- Debugging your app
- Distributing the app