views:

32

answers:

1

I'm trying to follow this tutorial on binding:

http://andrehoffmann.wordpress.com/2009/09/03/phonebook-tutorial-for-dummiesxcode-3-1-3/

but the app keeps crashing at run time.

In IB I have a NSBUtton, NSTableView, NSArrayController and a NSOBject (named AppController)

The tableview has 3 cols. Ad Name, Col and Height.

In AppController object I have an action titled LoadSheet and three outlets, btnLoadSheet, tblAdList and arrayController

In arrayController bindings I set it to AppController and set the Model Key Path to fileList, which is an NSMutableArray where I will be passing my table data.

In each table cell I bind them to arrayController and set the Model Key Path I set them to adName, adcol, adHeight respectively.

appController.h looks like this:

#import <Cocoa/Cocoa.h>

@interface AppController : NSWindowController {

    IBOutlet NSArrayController *arrayController;
    IBOutlet NSButton *btnAdRunner;
    IBOutlet NSButton *btnAddItem;
    IBOutlet NSButton *btnDelteItem;
    IBOutlet NSButton *btnLoadSheet;
    IBOutlet NSButton *btnQuit;
    IBOutlet NSTableView *tblAdList;

    NSMutableArray* fileList; 
}

@property (copy) NSMutableArray* fileList;

- (IBAction)AdRun:(id)sender;
- (IBAction)AddItem:(id)sender;
- (IBAction)DeleteItem:(id)sender;
- (IBAction)LoadSheet:(id)sender;
- (IBAction)QuitApp:(id)sender;
@end

appController.m:

#import "AppController.h"

@implementation AppController

@synthesize fileList; 

- (IBAction)LoadSheet:(id)sender {

    //define the ad array (this will be pulled from the run sheet
    fileList = [NSMutableArray arrayWithObjects:@"Ad1, 1, 2.5", @"Ad2, 1, 3", @"Ad3, 2, 1", @"Ad4, 1, 2.5", @"Ad5, 2, 4", nil];

    int i=0;
    for(NSString* thisdatarow in fileList) {

        //increase i
        i++;

        //make ad id
        NSString* adID = [NSString stringWithFormat:@"ad%d", i];

        //convert add data to an array from a string
        NSArray* templist = [thisdatarow componentsSeparatedByString:@","];

        //get ad items
        NSString* adname = [templist objectAtIndex: 0];
        NSString* adcols = [templist objectAtIndex:1];
        NSString* adheight = [templist objectAtIndex:2];

        //declare an array
        NSMutableArray* temparray = [[NSMutableArray alloc] initWithObjects:  adname, adcols, adheight];

        //dump temp array into arrayController
        [arrayController addObject:temparray];

    }

}

@end

I get what is going on in the larger picture but I'm lost on how to put it all together. I'm also not sure if I built it all correctly. For example, when binding arrayController to appController, the Model Key Path drop down only had self as an option, I had to manually add fileList, which kind of seemed like a red flag to me. Also, he's getting the Model Key Path for the table col binding from a plist, I'm, er, pulling them out o my butt. I am not going to be getting my data from a plist, just a text file, delimited by carriage returns and then commas. Do I dump that into a mutable dictionary so I can establish a key/value relationship and use the keys for my binding?

When the app compiles it comes out ok. Then I click the load sheet button and presto, spinning beach ball of death. I am assuming the app has no idea what I want it to do or resolve what I am asking it to do and decides to just die.

Any nudges in the right direction would be appreciated.

+1  A: 

In IB I have a NSBUtton, NSTableView, NSArrayController and a NSOBject (named AppController)

Don't you want your AppController instance in IB to be an instance of your AppController class? My apologies if that is already the case (then I'm just understanding your statement wrong).

For example, when binding arrayController to appController, the Model Key Path drop down only had self as an option, I had to manually add fileList, which kind of seemed like a red flag to me.

I think this is fine (someone correct me if I'm wrong). When you bind your arrayController to your appController's fileList attribute, your arrayController will then access the fileList by way of KVC mechanism. So as long as your appController class (or at least it's fileList attribute) is KVC-compliant, arrayController will be able to see appController's fileList just fine. Cocoa relies heavily on coding convention.

I'm also new to Cocoa, so I could be wrong about everything that I just said.

Yahya Cahyadi
yeah, I skipped that, my fault, AppController is an instance of my AppController class...
PruitIgoe
user480991: You have that exactly right.
Peter Hosey