简体   繁体   中英

How do I receive notifications outside of an NSView?

Disclaimer: I'm really new to ObjectiveC so I may have made fundamental errors in the structure of my app so please let me know if there's a better way to achieve my goal.

I'm trying to make an app that sits in the status bar like Dropbox. It should receive notifications. I have notifications working right now, but I have implemented them in an NSView that is in the application's window. I want to remove the window to be able to achieve a no window application (a la Dropbox). If I remove the window, it removes the view that I've created and then removes my application's ability to receive notifications.

Just in case I'm explaining this poorly, the notifications I'm receiving are received like this:

#import "test.h"    
@implementation test

- (id) initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    ...
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(windowChangeNotification:) name:NSWorkspaceDidActivateApplicationNotification object:nil];
    return self;
}

- (void)windowChangeNotification:(NSNotification *)note
{
    ...
}

And test is an NSView:

@interface appTracker : NSView {
    appTrackerDB * eventDB;
}

How can I receive notifications without having a window? Or is there a way to receive notifications without a view?

Extra info: I'm implementing the status bar UI component as an NSMenu, similar to this tutorial.

I'm pretty sure there is but I've gotten lost in the literature.

Thanks for the help!

Sure, every object in Objective-C can receive notifications, so just throw this code in your AppDelegate or wherever you find appropriate, maybe some model class.

(btw: class names start with a capital letter in ObjC by convention)

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