2
votes

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

1
Yep, it is closing... anyway I noticed that if I quite with the menu "Close App" it works good... so I'm changing the way my app will work it wont close when you click the red x button but will remain hidden and to close it you will need to use menu command... Thanks for your interest Peace MassyBlue

1 Answers

0
votes

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.