views:

517

answers:

2

Hey guys! =)

I've created in my CoreData app an entity with some attributes. Imagine a tableview and a bound NSArrayController. With both I create (and edit) my entity "instances". My question is how I can get the values of these attributes to my code. If there are more questions: http://twitter.com/xP_ablo

+1  A: 

If I have this correct, you have a TableView, bound to an NSArrayController which is bound to your data.

The simple way: Create an IBOutlet in your Class (usually the File's Owner) and in Interface Builder connect this IBOutlet to the NSArrayController. You can then get the values you need from this array.

Abizern
+2  A: 

You need to somehow get a reference to the NSArrayController. If you are loading the NIB yourself, you can add an IBOutlet instance to the class that is set as the NIBs "File Owner". When you load a nib, you supply the instance of the NIB's "File Owner" class as the owner. If you are not loading the NIB yourself (i.e. it's loaded automatically by Cocoa as the MaineMenu nib/xib of your app), then create an instance of your own class in the nib and add an IBOutlet to that instance. You create an IBOutlet in your class like this:

@interface MyClass : NSObject { //of course your class doesn't have to be a direct descendent of NSObject
    IBOutlet NSArrayController *arrayController;
}

@property (retain,nonatomic,readwrite) IBOutlet NSArrayController *arrayController;

...

@end

@implementation
@synthesize arrayController;

- (void)dealloc {
    [arrayController release];
    [super dealloc];
}
@end

Connect the IBOutlet in your class to the NSArrayController (controll-click on the File Owner in the first case or the instance of your class in the second case above) and drag to the NSArrayController. When you release the mouse, you'll get a pop-up of the IBOutlets in the drag source. Select the IBOutlet you created (e.g. "arrayController" in the example above).

One the nib is loaded (i.e. after awakeFromNib is called in your class), you can access the arrayController via the outlet:

NSArray *content = [[self arrayController] arrangedObjects];

and you can now do what you please with the values in the array.

Barry Wark
is it correct to say that you do not need the @property and @synthesize if you are not loading the nib yourself?
ctshryock
The NIB loading code will connect instance variables directly, but using the pattern shown above is the only one guaranteed to handle memory management correctly in both GC and non-GC environments, according to mmalc.
Barry Wark