Objective c cocoa如何设置状态栏应用程序左键单击事件?
我是objective-c编程新手,我现在正在尝试制作一个状态栏应用程序 我只知道如何设置一个下拉菜单,以便在单击状态栏项时显示 但是,我想要的是在左键单击时显示Objective c cocoa如何设置状态栏应用程序左键单击事件?,objective-c,macos,cocoa,Objective C,Macos,Cocoa,我是objective-c编程新手,我现在正在尝试制作一个状态栏应用程序 我只知道如何设置一个下拉菜单,以便在单击状态栏项时显示 但是,我想要的是在左键单击时显示面板,右键单击时显示菜单,就像酒保2的操作方式一样 我提到过这个,但我几乎不知道它有什么作用 我使用xib构建UI。我有三个.xib文件:主菜单,首选项和主面板 AppDelegate.h #import <Cocoa/Cocoa.h> @interface AppDelegate : NSObject <NSApp
面板
,右键单击时显示菜单
,就像酒保2
的操作方式一样
我提到过这个,但我几乎不知道它有什么作用
我使用xib
构建UI。我有三个.xib
文件:主菜单
,首选项
和主面板
AppDelegate.h
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property NSStatusItem *statusItem;
@end
我试着用
[菜单显示窗口];
但这是不对的。有关在给定的NSPoint
处绘制NSMenu
的信息,请参见帖子(使用popUpContextMenu:withEvent:forView:
而不是showWindow
)
我还将指出,在您链接的示例中,作者按照MVC模式将其项目模块化为不同的组件。菜单控制器和视图有一个组件(它看起来不像只显示一个NSMenu
),还有面板控制器和视图。您可能需要考虑如何按照MVC的惯例组织项目。有关在给定的n点绘制NSMenu
的信息,请参阅帖子(使用popUpContextMenu:withEvent:forView:
而不是showWindow
)
我还将指出,在您链接的示例中,作者按照MVC模式将其项目模块化为不同的组件。菜单控制器和视图有一个组件(它看起来不像只显示一个NSMenu
),还有面板控制器和视图。您可能想考虑如何按照MVC的惯例组织项目。是的,我正在尝试遵循MVC模式。但我还不太熟悉它,我正在做的是我做的第一个项目。我现在拥有的是一个AppDelegate
类和三个xib
文件,其中三个ViewController
连接到每个文件。但我很困惑,哪个类是响应我的点击操作的?AppDelegate
还是viewcontrollers?是的,我正试图遵循MVC模式。但我还不太熟悉它,我正在做的是我做的第一个项目。我现在拥有的是一个AppDelegate
类和三个xib
文件,其中三个ViewController
连接到每个文件。但我很困惑,哪个类是响应我的点击操作的?AppDelegate
还是视图控制器?
#import "AppDelegate.h"
#import "Menu.h" //Menu is a ViewController for Menu.xib
@interface AppDelegate ()
//@property (weak) IBOutlet NSWindow *window; //I don't know what is this for
@end
@implementation AppDelegate
@synthesize statusItem;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
//I initiate my statusItem here
-(void)awakeFromNib{
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
self.statusItem.title = @"T";
// you can also set an image
//self.statusBar.image =
self.statusItem.highlightMode = YES;
//I tried to use these code to set the left click action
[statusItem setTarget:self];
[statusItem setAction:@selector(showMenu:)];
}
-(void)showMenu{
Menu* menuVC = [[Menu alloc] initWithNibName:@"Menu" bundle:nil];
//Don't know what to do next...
}
@end