objective-c – 保存首选项以显示或隐藏NSStatusItem

objective-c – 保存首选项以显示或隐藏NSStatusItem,第1张

概述我有一个应用程序,它作为一个普通的应用程序运行,但也有一个NSStausItem. 我希望实现在偏好设置中设置复选框的功能,当此复选框打开时,应显示状态项,但是当复选框关闭时,状态项应被删除或不可见. 我发现有人在论坛中遇到类似的问题:How do you toggle the status item in the menubar on and off using a checkbox? 但是我对 我有一个应用程序,它作为一个普通的应用程序运行,但也有一个NsstausItem.
我希望实现在偏好设置中设置复选框的功能,当此复选框打开时,应显示状态项,但是当复选框关闭时,状态项应被删除或不可见.

我发现有人在论坛中遇到类似的问题:How do you toggle the status item in the menubar on and off using a checkbox?

但是我对这个解决方案的问题是它没有按预期工作.所以我做了这个复选框,一切正常,但是当我第二次打开应用程序时,应用程序无法识别我在第一次运行时所做的选择.这是因为复选框没有绑定到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 squareitem = [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 instancereturn 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.
不幸的是,这根本不起作用我做错了什么?

解决方法 您需要做的是使用 User Defaults系统.它使保存和加载首选项变得非常容易.

在按钮的 *** 作中,您将保存其状态:

- (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"];}

并且在您的app delegate(或其他适当的对象)awakeFromNib中,您将从用户默认值中读取该值:

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

然后确保在必要时调用removeStatusItem.

此过程几乎适用于您可能要保存的任何首选项.

总结

以上是内存溢出为你收集整理的objective-c – 保存首选项以显示或隐藏NSStatusItem全部内容,希望文章能够帮你解决objective-c – 保存首选项以显示或隐藏NSStatusItem所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1070478.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存