Database Reference
In-Depth Information
#import <Cocoa/Cocoa.h>
@interface
AppDelegate : NSObject
{
IBOutlet NSWindow *window;
IBOutlet NSImageView *imageView;
IBOutlet NSArrayController *recipeArrayController;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSManagedObjectContext *managedObjectContext;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator;
- (NSManagedObjectContext *)managedObjectContext;
- (IBAction)saveAction:sender;
- (IBAction)addImage:(id)sender;
@end
The
IBAction
, specifically
-(IBAction)addImage:(id)sender;
, is called from our main
menu and displays an open file dialog box. Along with this step, we need a
reference to the selected recipe in order to work with the recipe entities. To
accomplish this, we add a reference to the recipe's
NSArrayController
that is
instantiated in our nib within the
AppDelegate
.
Once the recipe's
NSArrayController
has been added to the
AppDelegate
header, we
need to go back to Interface Builder briefly and Control+drag from the
AppDelegate
to the recipe's
NSArrayController
to complete the binding.
While we are here, let's add a menu item to the File menu that will allow the
user to add an image for the recipe. We do this by making sure the MainMenu
element is open in Interface Builder (it appears as a floating menu) and
clicking its File menu. Next, we can either add a new
NSMenuItem
or use one
that already exists that is not being used. Since the Save As menu item is
not relevant to our application, let' s go ahead and rename it Add Recipe Image.
Once it is renamed, Control+drag from it to the
AppDelegate
, and bind the menu
item to the
IBAction
we defined in the header, as shown in
Figure 26,
Add Recipe
Image menu item
, on page 134
. With the bindings in place, it is time to imple-
ment the
-addImage:
method.
- (IBAction)addImage:(id)sender;
{
NSManagedObject *recipe = [[recipeArrayController selectedObjects] lastObject];
if
(!recipe)
return
;
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseDirectories:NO];
[openPanel setCanCreateDirectories:NO];
[openPanel setAllowsMultipleSelection:NO];