简体   繁体   English

在Mac OS X 10.8 Mountain Lion上未调用该方法

[英]Method does not invoked on Mac OS X 10.8 Mountain Lion

I'm trying to create small application which can be used over Mac OS X 10.8. 我正在尝试创建可在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. 在Mac OS X Mavericks和Mac OS X Yosemite上,我的应用程序运行良好,但在Mac OS X 10.8上,未调用应用程序的主要方法。 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. 我知道Mac OS X 10.8比之后的OS严格一些。 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. 我的错误是使用NSWindowDidEndSheetNotification作为工作表关闭通知。 [self getAudioCDPath] is a method which presents sheet OpenPanel and this have completionHandler: block in itself. [self getAudioCDPath]是一种呈现表单OpenPanel的方法,它本身具有completeHandler completionHandler:块。 Since what I'd like to do is just to invoke [self runEncodeAsync] , I should write it in the completionHandler: block. 因为我想做的只是调用[self runEncodeAsync] ,所以我应该将它写在completionHandler:块中。 No need to use NSWindowDidEndSheetNotification . 无需使用NSWindowDidEndSheetNotification

So, here's modified code of getAudioCDPath . 因此,这是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!!
    }
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM