保存偏好设置以显示或隐藏 NSStatusItem
我有一个作为普通应用程序运行的应用程序,但也有一个 NSStausItem
。 我想实现在首选项中设置复选框的功能,当打开此复选框时,应显示状态项,但当复选框关闭时,状态项应被删除或不可见。
我发现有人在论坛中面临类似的问题:如何使用复选框打开和关闭菜单栏中的状态项?
但此解决方案的问题是它无法按预期工作。因此,我勾选了此复选框,一切正常,但是当我第二次打开应用程序时,应用程序无法识别我在第一次运行时所做的选择。这是因为复选框没有绑定到 BOOL
或其他东西,复选框只有一个 IBAction
,它在运行时删除或添加状态项。
所以我的问题是:如何在首选项中创建一个复选框,以允许我选择是否应显示状态项目。
好吧,实际上我尝试了以下操作,我从帖子中复制了我在 AppDelegate.h 中给你的链接
:
NSStatusItem *item;
NSMenu *menu;
IBOutlet NSButton myStatusItemCheckbox;
然后在 Delegate.m 中:
- (BOOL)createStatusItem
{
NSStatusBar *bar = [NSStatusBar systemStatusBar];
//Replace NSVariableStatusItemLength with NSSquareStatusItemLength if you
//want the item to be square
item = [bar statusItemWithLength:NSVariableStatusItemLength];
if(!item)
return NO;
//As noted in the docs, the item must be retained as the receiver does not
//retain the item, so otherwise will be deallocated
[item retain];
//Set the properties of the item
[item setTitle:@"MenuItem"];
[item setHighlightMode:YES];
//If you want a menu to be shown when the user clicks on the item
[item setMenu:menu]; //Assuming 'menu' is a pointer to an NSMenu instance
return YES;
}
- (void)removeStatusItem
{
NSStatusBar *bar = [NSStatusBar systemStatusBar];
[bar removeStatusItem:item];
[item release];
}
- (IBAction)toggleStatusItem:(id)sender
{
BOOL checked = [sender state];
if(checked) {
BOOL createItem = [self createStatusItem];
if(!createItem) {
//Throw an error
[sender setState:NO];
}
}
else
[self removeStatusItem];
}
然后在 IBaction 中添加了这个:
[[NSUserDefaults standardUserDefaults] setInteger:[sender state]
forKey:@"MyApp_ShouldShowStatusItem"];
在我的 awakefromnib 中添加了这个:`
NSInteger statusItemState = [[NSUserDefaults standardUserDefaults] integerForKey:@"MyApp_ShouldShowStatusItem"];
[myStatusItemCheckbox setState:statusItemState];
然后在界面生成器中,我创建了一个新的复选框,将其与“myStatusItemCheckbox”连接,并添加了一个 IBaction,我还单击了绑定检查器,并在值中设置了以下绑定: NSUserDefaultController
和 ModelKeyPath
我设置:MyApp_ShouldShowStatusItem。
不幸的是,这根本不起作用,我做错了什么?
I've got an application which runs as a normal app but also has a NSStausItem
.
I wanted to implement the ability to set in the preferences a checkbox and when this checkbox is turned on the status item should be shown, but when the checkbox is off the status item should be removed or be invisible.
I found someone facing a similar problem in a forum here: How do you toggle the status item in the menubar on and off using a checkbox?
But the problem I have with this solution is that it does not work as expected. So I make this checkbox and all works fine, but when I open the application a second time the app does not recognize the choice I took at the first run. This is because the checkbox isn't bound to a BOOL
or something, the checkbox only has an IBAction
, which removes or adds the status item at runtime.
So my question is: how can I make a checkbox in the preferences which allows me to choose whether the status item should show up or not.
Ok actually i tried the following i copied the from the post i gave you the link
In AppDelegate.h :
NSStatusItem *item;
NSMenu *menu;
IBOutlet NSButton myStatusItemCheckbox;
and then in the Delegate.m :
- (BOOL)createStatusItem
{
NSStatusBar *bar = [NSStatusBar systemStatusBar];
//Replace NSVariableStatusItemLength with NSSquareStatusItemLength if you
//want the item to be square
item = [bar statusItemWithLength:NSVariableStatusItemLength];
if(!item)
return NO;
//As noted in the docs, the item must be retained as the receiver does not
//retain the item, so otherwise will be deallocated
[item retain];
//Set the properties of the item
[item setTitle:@"MenuItem"];
[item setHighlightMode:YES];
//If you want a menu to be shown when the user clicks on the item
[item setMenu:menu]; //Assuming 'menu' is a pointer to an NSMenu instance
return YES;
}
- (void)removeStatusItem
{
NSStatusBar *bar = [NSStatusBar systemStatusBar];
[bar removeStatusItem:item];
[item release];
}
- (IBAction)toggleStatusItem:(id)sender
{
BOOL checked = [sender state];
if(checked) {
BOOL createItem = [self createStatusItem];
if(!createItem) {
//Throw an error
[sender setState:NO];
}
}
else
[self removeStatusItem];
}
then in the IBaction i added this one :
[[NSUserDefaults standardUserDefaults] setInteger:[sender state]
forKey:@"MyApp_ShouldShowStatusItem"];
and in my awakefromnib i added this one : `
NSInteger statusItemState = [[NSUserDefaults standardUserDefaults] integerForKey:@"MyApp_ShouldShowStatusItem"];
[myStatusItemCheckbox setState:statusItemState];
Then in the interface builder i created a new checkbox connected it with "myStatusItemCheckbox" and added an IBaction also i clicked on the bindings inspector and set in the value the following bind to : NSUserDefaultController
and as ModelKeyPath
i set: MyApp_ShouldShowStatusItem.
Unfortunately this doesnt work at all what am i doing wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要做的是使用用户默认值系统。它使得保存和加载首选项变得非常容易。
在按钮的操作中,您将保存其状态:
并且在应用程序委托(或其他适当的对象)的
awakeFromNib
中,您将从用户默认值中读取该值:然后确保调用 <如果需要,请代码>removeStatusItem。
此过程几乎适用于您可能想要保存的所有首选项。
What you need to do is to use the User Defaults system. It makes it very easy to save and load preferences.
In the button's action, you will save its state:
and in your app delegate's (or another appropriate object)
awakeFromNib
, you will read that value back out of the user defaults:and then make sure to call
removeStatusItem
if neccessary.This procedure will apply to almost any preference you might want to save.