How to set NSStatusBar title from other class?
In one of my classes i setup my NSStatusBar like:
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setMenu:statusMenu];
Is it possible to somehow call something like:
[statusItem setTitle:@"Waiting for data..."];
From another class? I want to change the title as a user has entered information into a preferences window that is be开发者_StackOverflowing handled by a different class.
I tried to make a class function:
+(void)ChangeTitel
Which i called from the preferences class, it seemed to be able to call it but somehow not access the statusItem object.
Any ideas? :)
If you want to change the title from another class, that other class needs to have a reference to that status Item.
So what you have to do, is write a method that returns the pointer of this status item, ( its an instance variable right? ) and then call that method, to get the NSStatusItem object in the other class.
I think you are new to OOP coding, judging from your question. A class is a set of code.. sorta, if you [[Class alloc] init] you allocate some memory for the class, and create a new object of that class in it. A new instance. If you call something that starts with a - then you are calling an instance method, that requires you to have an instance. If you call something with a + you are calling a CLASS method, which has no instance, so no access to the instance variable of you status item.
- (NSStatusItem *)statusItem
{ return statusItem; }
Don't forget to declare this method in your header file as well, otherwise you will get a compiler warning.
Should be in the class that manages the status item. Then in the class where you want to use the status item:
#import "ManagerClass.h" // on top, so we have the method declared
Then:
ManagerClass *someInstanceToIt = [[ManagerClass alloc] init];
[(NSStatusItem *)[someInstanceToIt statusItem] setTitle:@"New Title"];
If the instance of the manager class is an interface builder outlet, or has been created before, then you shouldn't do that first line with alloc] init]
If you need any more help, post a comment.
精彩评论