简体   繁体   中英

unlockFocus called too many times

I click on a NSButton (openPane l) which is located on the main window of my OS X app to show a NSPanel (myPanel ) using the following method:

- (IBAction)openPanel:(id)sender {
    [_myPanel makeKeyAndOrderFront:nil];
}

Everything works as expected except I get the following description in the debug area only the first time I click on the button (openPanel):

"unlockFocus called too many times. Called on NSButton: 0x610000141080."

The attributes of the Panel are selected as follows:

Style: Utility Panel, 
Appearance:Title Bar and Shadow, 
Controls:  Close, 
Behaviour: Restorable, 
Memory: Deferred

I have looked through the web but can not find any explanation. Does anyone know why this happens or how to resolve it?

I had the same problem using XCode 7.0 beta 6 using storyboards and a segue to display a sheet. It seems some common problem which happens if you open the panel directly from the action.

To solve the problem, just open the panel from the main queue:

- (IBAction)openPanel:(id)sender {
    dispatch_async(dispatch_get_main_queue(), ^{
        [_myPanel makeKeyAndOrderFront:nil];
    });
}

This will also solve similar problems from swift and storyboards. Use this swift code to call a segue:

@IBAction func onButtonClicked(sender: AnyObject) {
    dispatch_async(dispatch_get_main_queue(), {
        self.performSegueWithIdentifier("identifier", sender: self);
    })
}

Not sure what is going on there but I would try this to show your NSPanel:

-(IBAction)openPanel:(id)sender {
[[NSApplication sharedApplication] beginSheet:_myPanel
                               modalForWindow:self.window
                                modalDelegate:self
                               didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
                                  contextInfo:nil];

Also you should make sure that Release When Closed checkbox is activated for your NSPanel. Search for it in the Interface Builder.

EDIT : Are you drawing something on your NSPanel ? Setting a NSColor or showing a NSImage ? My guess is that you are displaying a NSImage and messed something up concerning this: Apple Doc: lockFocus

I have fixed the same problem with the following

In Header

@interface aWindowController : NSWindowController
{
    /* other Vars */

    NSOpenPanel *panel;
}

In .m

- (void)windowDidLoad {
    [super windowDidLoad];

    /* other stuff */

    /* set Panel Properties */
    panel = [NSOpenPanel openPanel];

    [panel setCanChooseDirectories:YES];
    [panel setCanChooseFiles:NO];
    [panel setAllowsMultipleSelection:YES];
}

in NSButton IBACTION

- (IBAction)getFiles:(id)sender
{
    NSArray *resultArry;

    if([panel runModal] == NSFileHandlingPanelOKButton ){
        resultArry = [panel URLs];
        return resultArry;
    }

    return nil;
}

ARC will clean up the panel.

I had the same problem with a NSPopUpButton that called a Segue and @Flovdis answer worked for me.

This is the Objective C code I used:

dispatch_async(dispatch_get_main_queue(), ^{
        [self performSegueWithIdentifier:@"MySegue" sender:self];
    });

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.

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