Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4248810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:18:42+00:00 2026-05-21T04:18:42+00:00

In the application I am creating, I have a welcome window with a list

  • 0

In the application I am creating, I have a welcome window with a list of recent documents (similar in functionality to the new Xcode 4’s welcome window). I am registering both the application’s delegate and a view controller in the welcome window for posted NSWindowWillCloseNotification. Unfortunately, only the applications delegate is ever getting notified of this event.

I’ve tried the following, all with the same behavior (the window controller is not notified):

  • Removing the AppDelegate’s notification registration code, hoping that somehow it was ‘consuming’ the notification.
  • Changing the method on the view controller to -(void)windowIsClosing: such that it isn’t the same name as the app delegate (quite the long shot, but I had to try)
  • Moving the addObserver:... call in the ViewController to else where in the code (so its not invoked during the initializer, if somehow that mattered).
  • I do de-register my view controller from the notification center during its dealloc method, but I’ve ensured that the dealloc method is getting called after the window is closed not during the closing.

I also tried for listening to other events, such as NSWindowWillMoveNotification in both the delegate and the controller, and once again the delegate gets notified but not the view controller. My view controller is not part of the first responder chain, but that shouldn’t matter since I’m registering for a notification not looking to handle nil-targetted actions.

Therefore, why is my controller not being notified of the window closing events but my app delegate is?

The relevant code is as follows….
App Delegate:

@interface AppDelegate : NSObject <NSApplicationDelegate> {
}
@end

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(windowClosing:) 
                                                 name:NSWindowWillCloseNotification 
                                               object:nil];
    // other initialization stuff
    [self showWelcomeWindow];
}

- (void)windowClosing:(NSNotification*)aNotification {
    // this method gets called when any window is closing
}
@end

Controller:

@interface ViewController : NSObject {
}
@end

@implementation ViewController
- (id)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(windowClosing:)
                                                     name:NSWindowWillCloseNotification
                                                   object:nil];
    }
    return self;
}

- (void)windowClosing:(NSNotification*)aNotification {
    // this method does not called when window is closing
}
@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:18:42+00:00Added an answer on May 21, 2026 at 4:18 am

    Answering my own question for posterity now that I’ve figured it out.

    As stated in the NSNotificationCenter documentation:

    Be sure to invoke removeObserver: or removeObserver:name:object: before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated.

    The ViewController object was listening to notifications for windows that are closing (NSWindowWillCloseNotifications) and to one of my data model object’s notifications.Therefore when I was setting a different model object on the controller I was de-registering the ViewController from listening to the model object that was being replaced.

    Unfortunately, I chose to use the removeObserver: method (which also removed the object from being notified of window closing events) rather than the more specific removeObserver:name:object: to remove my controller from only a subset of the notifications that the object had registered for. Looking back on the code, the removeObserver was written before the controller object had any need to be notified of events originating from anything other than the model.

    The moral of the story is to have the mental discipline to only use [[NSNotificationCenter defaultCenter] removeObserver:self] during the object’s dealloc call and to otherwise de-register from very specific events (since you cannot know down the road what other event notifications the object will be registered for as well).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# Application I am creating that stores all data in SQL
I am creating an application in which I have two mxml components, MainPanel.mxml and
When creating a web application, and lets say you have a User object denoting
We are creating an XBAP application that we need to have rounded corners in
I am creating an touch screen application using Swing and have a request to
I have a C application that I've created in VS2008. I am creating a
I am new to iphone development.I am creating a map application.Now i am facing
I am creating custom membership provider for my asp.net application. I have also created
I am creating an application using the MVVM pattern and somewhere I have a
I'm creating an application which lets you define events with a time frame. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.