Pergunta

I have NSMenu of a status bar app, defined in AppDelegate.h like this:

IBOutlet NSMenu *spotMenu; 

@property (nonatomic, retain) IBOutlet NSMenu *spotMenu;

And in AppDelegate.m like this:

[spotApp setMenu: spotMenu];


- (NSMenu *) spotMenu
{
return spotMenu;

}

I am trying to access it from another class like this:

AnotherClass.h

@class AppDelegate;

AppDelegate *appD;

AnotherClass.m

#import "AppDelegate.h"

NSLog(@"%@", [appD spotMenu]);

It returns null.

2012-09-23 10:23:37.543 Spot[581:303] (null)

When I do the same from within AppDelegate class like this:

NSLog(@"%@", spotMenu);

It returns correct content:

2012-09-23 10:56:05.460 Spot[679:303] <NSMenu: 0x100618030>
Title: 
Supermenu: 0x0 (None), autoenable: YES
Items:     (
    "<NSMenuItem: 0x100618170 About Spot>",
    "<NSMenuItem: 0x10011b9b0 Preferences>",
    "<NSMenuItem: 0x1001b9390 >",
    "<NSMenuItem: 0x1001bbe70 >",
    "<NSMenuItem: 0x1001b99a0 Quit Spot>"
)

Any way I can get the same output from another class as from AppDelagate class?

Thank you.

Foi útil?

Solução

To access your App Delegate you should do this:

AppDelegate* appD = (AppDelegate *)[[NSApplication sharedApplication] delegate];

or

AppDelegate* appD = (AppDelegate *)[NSApp delegate];

Then you can reference your NSMenu from appD.spotMenu

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top