The identity inspector is useful for connecting a UI object to a custom subclass, or for associating an object with a key value pair. This video shows how to do those things.
- [Instructor] Let's talk about the identity inspector. The identity inspector is that button to the left of the attributes inspector in the inspector bar. Using the identity inspector, the most common use is actually to set a custom class for a selected object. For example, if I filter for a label in the object library and click and drag it out, with it selected, I can look in the identity inspector and see that the class is NSTextField. If I wanted to subclass NSTextField, I could create that class, and then I can choose it from the dropdown here or type the subclass name in here.
Also with this class area, you can click this arrow button, and that'll take you to the definition of that class. So if you have a subclass that you're working in, this can be really useful to quickly get into that subclass and edit it and jump back into Interface Builder. I'm going to hit the Back button in the jump bar to go back to Interface Builder now. In addition to that feature in Interface Builder, I want to point out a few more. There's this identity area where you can set an I.D., so you can sit an identifier to a user interface object.
You can give it a tool tip. For accessibility, you can give it a description. Give it some help text. Under the documents section, you can give it a specific label if you want or set a color to it. But what I feel is most interesting here is this user-defined runtime attributes section where we can associate key value pairs and bind those key value pairs to objects in our code. Let's look at how that works.
I'm going to go to the assistant editor by showing the navigator and option clicking AppDelegate.swift. In here I'm just going to right click and drag from my label into my code between lines 12 and 13. For the connection, I have outlet type is NSTextField. I'll simply call this label ask hit Connect. Let's go back to the standard editor. And then go to AppDelegate.swift. Here I'm going to create a new property. On line 16, I'll call this name.
It's going to be a string. And I'm going to set it equal to not set in brackets. Doesn't really matter what you call this. Just put something inside of the string. Inside applicationDidFinishLaunching, I'm going to set the label.stringValue to name. So if we run the application now, we should see that our code actually overrides what we see in Interface Builder. So it doesn't have that default text on it. It says not s. And the reason why it doesn't say not set is because we actually have to stretch out the label to accommodate text.
So if you're going to add text in your code, you want to stretch out the label a little bit by grabbing the handle. And again, I'm grabbing the right center handle, so I'm just expanding horizontally, and the left is staying anchored to the left. So we have our label. When we run the app, it says not set. Back into Xcode. Let's look at that key value pair feature. So, right here, I'm actually not going to select the label and modify these user-defined runtime attributes.
I'm actually going to select the delegate. That's this cube right here. You can also use the document outline and just click on Delegate. So, what you want to see in the identity inspector is the class is your AppDelegate class. Now, the reason why it's not grayed out is because this is actually a custom class, A class that's part of your project. The NSTextView class is part of the built-in Mac OS framework. That's why it's grayed out, because it's not something that you have have created.
So, in user-defined runtime attributes, we're actually going to connect our name property to a key value pair. So I'll add a key value pair by clicking the plus button. Change the key path to our property name, which is name. Change the type to match the property type. And then the value, we can type whatever we want. We'll type KVP, short for key value pairs, and an exclamation point. So, what this is going to do when the application runs is it's going to modify the name attribute which belongs to AppDelegate.
We can see that here. We had this name property right here. It's going to modify that at runtime. Go back to the zip file. And by modifying that at runtime, we can actually control our properties in our code through Interface Builder. So if I test the application now, the value here inside of Interface Builder actually overrides what's in the code. So we can use these user-defined runtime attributes when we're on the selected object.
So, since name belongs to AppDelegate, we can modify its values right here. And then, of course, if we choose to manipulate our user interface objects with those properties as I'm doing here on my line 20 in my code, we can modify values of UI objects at runtime through these key value pairs. So we have control over putting that data inside of a label, for example, or using it for anything else. But we have control over it in Interface Builder through the identity inspector.
Released
11/18/2016These Xcode tutorials help new developers install Xcode and start writing and editing code. Instructor Todd Perkins shows how to build a brand-new user interface (UI) with buttons and menus in Interface Builder, Xcode's intuitive UI design tool, as well as advanced designs that adapt to screen size, aspect ratio, and orientation changes. He reviews the version control and storyboard features, as well as the basics of schemes and behaviors. Plus, learn how to compile and debug apps, test apps in the iOS Simulator, and send your app to Apple for distribution in the App Store.
- What is Xcode?
- Installing Xcode 8
- Creating your first Xcode project
- Editing code
- Creating snippets
- Adding Git version control
- Making interface connections to the code
- Using Interface Builder
- Creating storyboards
- Pinning objects
- Compiling code with the LLVM compiler
- Working with schemes and behaviors
- Sharing resources in a workspace
- Catching common errors with Analyze and Fix-It
- Using the iOS Simulator
- Preparing an app to be published
Share this video
Embed this video
Video: Use the Identity Inspector