PDA

View Full Version : [self close] on NSWindowController subclass does not close the window




SP-NewToMac
May 21, 2009, 12:38 PM
Hello,

Apologies for the long email. Don't know enough about Mac development to ask the right questions. So want to give as much info as possible to describe my issue.

I have one main dialog which has three buttons that open three Panel windows. I have Controller classes (subclasses of NSWinController) for each of these panels that have closeButtonClicked method which simply calls [self close];

One of the three dialogs opens and closes fine. The other two dialogs just ignore the [self close] command. The panel stays open after clicking the close button. I have to use the red close button on the window bar to get rid of the window.

I see absolutely no differences in the controller code for the three panels and I followed the same method in creating the panels step by step as much as I can remember. Why do they behave differently?

Here is the main window controller code which has actions associated with three buttons on the main form.

- (IBAction)sendReport:(id)sender
{
[self updateUserDefaults];
NSString *steps = [tFieldStepByStep stringValue];
NSLog(@"Step By Step is %@", steps);
if (!progressDialog) {
progressDialog = [[ProgressDialogController alloc] init];
}

NSLog(@"Show progress pane ");
[progressDialog showWindow:self];
//Build the XML
[self buildDmpInfoXML];
}

- (IBAction)viewReportDetails:(id)sender
{
if (!cerDetailsController) {
cerDetailsController = [[CERDetailsController alloc] init];
}

NSLog(@"Show Details pane %@", cerDetailsController);
[cerDetailsController showWindow:self];
}

- (IBAction)viewExampleDescription:(id)sender
{
if (!exampleDescController) {
exampleDescController = [[ExampleDescriptionController alloc] init];
}

NSLog(@"Show Example pane %@", exampleDescController);
[exampleDescController showWindow:self];
}

Each of the panel controller headers have code very similar to this

#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>

@interface ExampleDescriptionController : NSWindowController {
//IBOutlet NSTextField *tFieldExampleDescription;
IBOutlet WebView *webview;
}

-(IBAction)closeButtonClicked:(id)sender;
@end

Each panel controller has init method that loads the nib, and awakeFromNib has some logic to display right content.

-(id)init
{
if (![super initWithWindowNibName:@"ExampleDescription"])
return nil;
return self;
}

closeButtonClicked has following code.

-(IBAction)closeButtonClicked:(id)sender
{
[self close];
}

Why don't the windows close when I click on close button?

Thanks in advance for your help.



kainjow
May 21, 2009, 06:45 PM
Code looks fine to me. Did you double-check your IB connections?

BTW, your init method could be simplified to this :)
-(id)init
{
return [super initWithWindowNibName:@"ExampleDescription"];
}

SP-NewToMac
May 22, 2009, 12:33 AM
The connections are fine because the [self close]; command gets executed after I click on the button. It just doesn't close the window. After struggling for a few hours I gave up and added [self autorelease]; command in closeButtonClicked method. But then I have to force [[CERDetailsController alloc] init]; every time the button gets clicked. It seems like a waste to kill the window completely and reinitialize every time, but works for now.

Also how do I kill the parent window from the panel? After closing one of the panels, I want to close the application completely. I added following code to the close method. It did not terminate the app.
[self close];
[self autorelease];
[NSApp terminate];
I obviously have a lot of reading to do here. I am probably not using the right controls and APIs. :)

Thanks for the tip on init. Makes sense.

kainjow
May 22, 2009, 12:53 AM
Aren't you getting a compiler warning for [NSApp terminate], and an error at runtime? The method is terminate:, so it should be [NSApp terminate:nil]

I'm wondering if the bug is in your NSWindowController nibs.. maybe you have duplicate controllers. Can you post a screenshot of ExampleDescription.nib?

SP-NewToMac
May 22, 2009, 01:18 AM
I did get an error. So I have removed the terminate command since. :P

Screenshots are attached. DetailsController is the one that does not close. Example description closes.

kainjow
May 22, 2009, 09:38 AM
Sorry, I meant the main nib window, where all the top-level objects are (Files Owner, windows, controllers, etc.). Sometimes weird bugs happen when multiple instances of a controller are created in a nib, so I just want to see if that could be the problem here.