I'm trying to create small application which can be used over Mac OS X 10.8.
On Mac OS X Mavericks and on Mac OS X Yosemite, my application work well but on Mac OS X 10.8, main method of my application does not invoked. To be exact, the method seems to be invoked momentary, but soon after killed with alert sound. I know that Mac OS X 10.8 is a bit strict than the OS after it. But I think there are something wrong with my code which I can't find out.
My main method is OK because if it is called directly, it works fine without any problem. But if I call it in the method of notification selector, the problem occur.
Here's my code. I appreciate any kind of suggestion,thanks.
- (IBAction)startButton:(id)sender {
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
if ( [[defaults objectForKey:@"aBookMark"] length] == 0 ) {
[self getAudioCDPath];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(runEncodeAsyncWithNotify:)
name:NSWindowDidEndSheetNotification
object:self.window];
} else {
[self runEncodeAsync];
}
}
/* This method have problem */
-(void)runEncodeAsyncWithNotify:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSWindowDidEndSheetNotification
object:self.window];
encodingFlag = YES;
[_start setEnabled: NO];
[_stop setEnabled: YES];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
[self encodeWithLAME];
}];
}
/* This method does not have any problem */
-(void)runEncodeAsync {
encodingFlag = YES;
[_start setEnabled: NO];
[_stop setEnabled: YES];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
[self encodeWithLAME];
}];
}
My mistake is to use NSWindowDidEndSheetNotification
as sheet close notification. [self getAudioCDPath]
is a method which presents sheet OpenPanel and this have completionHandler:
block in itself. Since what I'd like to do is just to invoke [self runEncodeAsync]
, I should write it in the completionHandler:
block. No need to use NSWindowDidEndSheetNotification
.
So, here's modified code of getAudioCDPath
. Sorry I don't show it in the question.
[aPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSURL *aDirURL = [[aPanel URLs] objectAtIndex:0];
NSData *bookmark = nil;
NSError *error = nil;
bookmark = [aDirURL bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
includingResourceValuesForKeys:nil
relativeToURL:nil
error:&error];
if (error) {
NSLog(@"Error creating bookmark for URL (%@): %@", aDirURL, error);
}
[defaults setObject:bookmark forKey:@"aBookMark"];
[self runEncodeAsync]; // I added this to invoke main method!!
}
}];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.