Challenge 2 problem


#1

I’m using XCode 4.2 on Lion, and trying to do challenge 2, but I just can’t figure out how to do this in the UI. In the text for challenge 2, it says “Then the RMDocument class will be the dataSource of the Table View”…

Do I have to manually set that up, or did it get done for me when I set the identifier for the column? When I ctrl-click on the table view, I see that there is a data source bubble, but it is not connected anywhere, and I can’t figure out how (or if I need to) to connect it to RMDocument. I did figure out how to set the button targets doing something similar to chapter 4 (?) by using the assistant editor.

How do I set the data source for the table to be the RMDocument class?


#2

Take a break and make yourself a nice cup of coffee or your favorite hot beverage, and enjoy it.

Then go to NSArrayController Class Reference.

Under Companion guides, go and read Cocoa Bindings Programming Topics.

And make sure that you understand what you read.

Ditto for NSTableView Class Reference and its Companion Guide Table View Programming Guide.


#3

Thanks for the reply! I still have a few questions, though.

It took awhile, but I read both chapters and the referenced guides in the OSX documentation completely, but they don’t answer my question, perhaps I asked it poorly. (Also possibly the topics have changed to no longer discuss the details that I needed, the document viever went to the web and chose different context than the index of the document viewer displayed.) My biggest problem is just figuring out the UI enough that I know how to connect A to B, and the suggested reading didn’t cover that in much detail.

Once I have all the code entered, I’m having problems figuring out

  1. What should connect to what
  2. How to make the connections in the UI

For 1), I think that I need to connect the table in the XIB to the data source in RMDcument. I think that I need to connect the columns to the fields of the person object.

For 2), problems making the connections: When I select the table, I need to bind it to RMDocument.m’s data source methods. Is that represented as “File Owner”? Is it correct to set the dataSource and delegate methods to FileOwner? It seems that the chapter with a good explanation of FileOwner is later in the book than I have gotten, but I’m trying to be disciplined and complete the challenges before I proceed. I tried setting dataSource and Delegate of the table in the XIB to FileOwner by double clicking on the table and control dragging to the FileOwner object, I hope that is right. I then tried dragging from the table to RMDocument.h as we did in a previous chapter (5) to make a tableView object.

Then, I need to bind the columns to the fields. I try selecting a column, and in the bindings inspector, I choose to bind to file owner, check the checkbox, and I would very much like to set the controller key to employees, and the ModelPathKey to personName, but what I see is that I can’t edit the controller key until I put a valid value into ModelPathKey, and it won’t take person name. It will take “tableView”, but none of the completions seem to get me to personName.

Now, I’m confused and can’t figure out how to proceed.

The binding process seems a bit too magical - while I appreciate that it is doing work for us behind the scenes, I don’t really know how to use the magic, and I haven’t found a good reference for it in my forays into the documentation and web searches. The book is wonderful in handholding us for each connection that we need to make, but I haven’t yet discerned the laws behind what it tells us to do well enough to do it myself yet.


#4

If I were you, at this stage of learning, I would not focus on finding the answer to a specific question but I would focus on learning the fundamentals first. So when reading the Apple’s programming guides, forget about the specific question you have in mind and concentrate on the material being presented. (I know how distracting it can be if you are looking for a specific answer.)

To motivate you further, here is a simple Cocoa bindings example to display in a (single-column) table some words stored in an array.

AppDelegate.h:

//  AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (strong) NSArray *primesArray;

@end

AppDelegate.m:

//  AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize primesArray;

@synthesize window = _window;

- (void)dealloc
{
    self.primesArray = nil;
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    self.primesArray = [NSArray arrayWithObjects:@"Two", @"Three", @"Five", @"Seven", @"Eleven", @"Thirteen", @"Seventeen", nil];
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
    return YES;
}

@end

Here is what you do in Xcode:

  1. Create a simple non ARC Cocoa Application and replace the AppDelegate files with the ones shown above.
  2. Click on the MainMenu.xib file.
  3. Select the Window and then its content view; add an NSTableView (Content Mode: Cell Based) to the view, and configure the table a have only one column.
  4. Add an Array Controller to the XIB file and name it Primes Array Controller. (Array Controllers can be found in ObjectLibrary/Objects and Controllers - Do: View/Utilities/Show Object Library)
  5. Select the Primes Array Controller and Do: View/Utilities/Show Bindings Inspector.
  6. Bind the Primes Array Controller’s Content Array to App Delegate; make sure that you also enter primesArrayin the Model Key Path (remember that primesArray is App Delegate’s property).
  7. Select the TableView’s (one and only) column, and bind its Value to PrimesArrayController. Make sure that Controller Key is set to arrangedObjects and Model Key Path is set to self.
  8. Go and build the curent scheme and run it.
    Now you should see a table view containing the names of the first seven prime numbers.

Bindings are very powerful, but it takes some effort and stamina to learn them.


#5

Thanks again! I appreciate the help!

However, in this case, for challenge 2, I need to figure out how to connect without using the NSArrayController, so the sample you have posted (and I appreciate you going to the effort of including everything I need!) pretty much duplicates what is already in chapter 8.

I think that I’ll just skip the challenge for now, and come back to it after I have read more of the book, hoping to find my insight with doing bindings in a later chapter, or hoping I finally run across it in the apple docs.