Database Reference
In-Depth Information
Figure 26—Add Recipe Image menu item
The implementation of -addImage: displays an NSOpenPanel , which attaches to the
window as a sheet, making it modal to the window. Next, we tweak the NSOpenPanel
a little bit so it cannot select directories or multiple files or create directories.
Notice that we check to make sure a recipe has been selected before we open the
panel. Otherwise, without a recipe, we would have nothing to associate the image
with. A little bit of error checking can go a long way.
Since sheets work asynchronously, we need to add a completion block. The
completion block will be called once the user is done interacting with the
NSOpenPanel .
Shared/Desktop/AppDelegate.m
[openPanel beginSheetModalForWindow:window
completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelCancelButton) return ;
NSURL *fileURL = [[openPanel URLs] lastObject];
NSError *error = nil;
//Build the path we want the file to be at
NSURL *destURL = [NSURL fileURLWithPath:[self applicationSupportFolder]];
NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString];
destURL = [destURL URLByAppendingPathComponent:guid];
BOOL success = [[NSFileManager defaultManager] copyItemAtURL:fileURL
toURL:destURL
error:&error];
NSAssert2(success, @ "Error copying file: %@\n%@" ,
[error localizedDescription], [error userInfo]);
[recipe setValue:[destURL path] forKey:@ "imagePath" ];
}];
 
 
 
Search WWH ::




Custom Search