Question

I'm having a problem closing my app after NSSavePanel has been used... If I open the app, work with it and then close it, it closes in the right way... I have this code in the appdelegate to make my app close

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
 {
   return YES;
 }

but then if I just open the panel to save a txt file... with NSSavePanel... also if I just open it and don't use it... clicking the "Cancel" button, my app doesn't close anymore... if I click the red x to close the app the window disappear but the app remain active and there's no way to bring the main window back up. The code I'm using to save my txt files is this:

NSSavePanel *save = [NSSavePanel savePanel];

if (nomePartita != nil) 
{
    [save setNameFieldStringValue:nomePartita];
}

[save setAllowedFileTypes:[NSArray arrayWithObject:@"dat"]];
[save setAllowsOtherFileTypes:NO];

NSInteger result = [save runModal];    

if (result == NSOKButton)
{
 // code to save the file here.....
 }

problem must be here in this lines because just with this (I mean with out the rest of my code for creating and saving the txt file) I have this problem... Anyone has a clue why is this happening? Any hint will be very much appreciated! Thanks a lot, Peace, Massy

Was it helpful?

Solution

Try this:

   NSSavePanel*    panel = [NSSavePanel savePanel];
   [panel setNameFieldStringValue:newName];
   [panel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result){
        if (result == NSFileHandlingPanelOKButton)
        {
            NSURL*  theFile = [panel URL];
            // Write the contents in the new format.
        }
}];

I faced the same issue and this solved it. For window you can alternatively use [NSApp keyWindow]

Apple guideline with sandboxing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top