Mac GeekeryGet your geek on. |
|
blog advertising is good for you
recent popular content
User login
|
So you’ve toyed with making a simple database and quickly found that you’d like to have the equivalent of multiple “tables” in your database. A reasonable request, and something that Core Data can do, again without coding. It’s not perfect, but it’s functional. What we’ll do is take the previous call logger project and add in the ability to handle People entities and choose from them for the caller and the called. A little more work and we can see which messages were left for any given caller, but that will be left for a later article as it requires a little more effort than is done here, and I’d rather not throw it all out at once for the sake of leaving it digestible. So if you haven’t gone through the previous walkthrough, now would be a good time as we’re making edits to that project. After making these changes your old data files will not work properly and may even crash the program. Add a Person Entity
Open up the project and select the model so we can edit it. In the Entities list add a Person object and give it two strings for properties: name and mainPhone. We’ll add more later, but this is enough to get started. Add the Relationships
In the Call entity, remove the “caller” and “messageFor” properties. Now, add two new relationships and call them “caller” and “called” and set them up with the destination of “Person” — leave the rest alone, we want a standard relationship. What did we just do? We told the Call entity that we would not enter people in manually but, rather, would fill these positions with existing People entities. This presents two problems: how do we make these People entities and how do we select them? As in the first article, creating the interface is easy. That second point will require some effort, but not a lot. Add the InterfaceOpen your interface in Interface Builder and double the width of the window. Drag the People object in as you did last time for Call and arrange the two parent boxes so that they are side-by-side. Now you can edit People objects.
Now for the trick in selecting them. Here we use a Panther technology called Bindings to bind a menu to the data controller and, thus, populate the menu with what we need. For us, all we need to do is pick a few things from a menu. Double-click in an unused area of the Call box in Interface Builder so that the box gets a heavy selection border. Now your focus is on the objects inside of it. You may try this a few times before you realize that not all you thought was empty was empty. If you have problems, aim between the buttons or to the left of the search field. Now select and remove the while text field next to “Caller” and “Message For” while leaving the labels. In the palette choose the Controls tab with the button and slider. Drag in the menu from the middle of this palette to the right of the Caller label. Resize it so it’s as wide as the text fields. With this item selected, press ⌘⇧I to get the Inspector palette and from the menu within choose Bindings. If the title of the palette doesn’t say “NSPopUpButton” then click on the menu to select it again. We’re going to set the menu to get a list of objects from Core Data, show their names, and then set the value in Call to whatever object corresponds to what we selected. It’s easier than it sounds. To tell the menu what objects to use, click on “content” and set it as follows:
Then to tell it what names to show, click on “content value” set it as:
Finally, to tell it where to put the value you chose, under “selectedObject” set it to:
Drag in another menu and do the same for the “called” property, binding it to the model key path “called” in the last step. Cleanup and RunNow let’s clean some things up from the previous project so that this works right. Double-click on the table columns for “Caller” and “Message For” a few times until one is selected and you can edit its bindings. Change the bound model key path for caller to “caller.name” and for “Message For” to called.name. You can change the name of the columns if you wish. Save, go back to Xcode, and run. Now add a few people in on the right and then add a new call on the left. You can choose from those people you added in the menus now and their name shows in the table. Window resize is a little funky, and it’s a little cumbersome, but it’s functional. Later on I’ll talk about reverse lookups so you can see who is linked to you and display that (what messages are waiting for Bob?). [series-info:center]
About Adam Knight
Author Biography Adam Knight is one of the founders of Mac Geekery and is a geek at heart. Programmer by day, hacker by night, his daily life revolves around the Macintosh platform, which he has been a user and programmer for since the early days of System 7 when his LCII replaced his Apple //c. In-between tech jobs, he’s managed to learn the basics of any web hacker: PHP, MySQL, Perl, Apache, Linux, *BSD, and the intricacies of ./configure —prefix=~/bombshelter/. Today, codepoet is concentrating on blogging again, writing some software for the Mac by himself (including Notae) and for his company (such as Switchblade) and has a few other toys coming out soon. Bug him over AIM or email [link fixed]. |
Hi Adam, I’ve been delving into core data applications recently (with plenty of help from your two how-tos), and I’ve run into a problem I haven’t been able to figure out how to solve. My project is essentially the same as yours. To use terms from your project I’d like to have a search bar where I can type in a name and have the table on the left in your interface show me all of the calls that match that name. I’ve successfully made search bars that search through the call entity and return every entry with a particular time for example (though this search bar is made automatically in your example). I would really appreciate any help you could give me.
thanks!
Jacob