简体   繁体   English

保存首选项以显示或隐藏NSStatusItem

[英]Save preference to show or hide NSStatusItem

I've got an application which runs as a normal app but also has a NSStausItem . 我有一个应用程序,它作为一个普通的应用程序运行,但也有一个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. 这是因为复选框没有绑定到BOOL或其他东西,复选框只有一个IBAction ,它在运行时删除或添加状态项。

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 : 在AppDelegate.h中:

 NSStatusItem *item;
NSMenu *menu;
IBOutlet NSButton myStatusItemCheckbox;

and then in the Delegate.m : 然后在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 : 然后在IBaction我添加了这个:

[[NSUserDefaults standardUserDefaults] setInteger:[sender state]
                                               forKey:@"MyApp_ShouldShowStatusItem"];

and in my awakefromnib i added this one : ` 在我的awakefromnib中,我添加了这个:`

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. 然后在界面生成器我创建一个新的复选框连接它以“myStatusItemCheckbox”和添加了一个IBAction为还我点击了绑定检查,并在值以下绑定设置为: NSUserDefaultController和作为ModelKeyPath我设置: MyApp_ShouldShowStatusItem. Unfortunately this doesnt work at all what am i doing wrong ? 不幸的是,这根本不起作用我做错了什么?

What you need to do is to use the User Defaults system. 您需要做的是使用User Defaults系统。 It makes it very easy to save and load preferences. 它使保存和加载首选项变得非常容易。

In the button's action, you will save its state: 在按钮的操作中,您将保存其状态:

- (IBAction)toggleStatusItem:(id)sender {

    // Your existing code...

    // A button's state is actually an NSInteger, not a BOOL, but
    // you can save it that way if you prefer
    [[NSUserDefaults standardUserDefaults] setInteger:[sender state]
                                               forKey:@"MyApp_ShouldShowStatusItem"];
}

and in your app delegate's (or another appropriate object) awakeFromNib , you will read that value back out of the user defaults: 并且在您的app delegate(或其他适当的对象) awakeFromNib ,您将从用户默认值中读取该值:

 NSInteger statusItemState = [[NSUserDefaults standardUserDefaults] integerForKey:@"MyApp_ShouldShowStatusItem"];
 [myStatusItemCheckbox setState:statusItemState];

and then make sure to call removeStatusItem if neccessary. 然后确保在必要时调用removeStatusItem

This procedure will apply to almost any preference you might want to save. 此过程几乎适用于您可能要保存的任何首选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM