我終於開始測試它,它完美的工作,所以你的代碼肯定有問題。
這裏有一個自定義視圖,允許拖動:
@implementation DragStatusView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//register for drags
[self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
//the status item will just be a yellow rectangle
[[NSColor yellowColor] set];
NSRectFill([self bounds]);
}
//we want to copy the files
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
return NSDragOperationCopy;
}
//perform the drag and log the files that are dropped
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if ([[pboard types] containsObject:NSFilenamesPboardType]) {
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
NSLog(@"Files: %@",files);
}
return YES;
}
@end
這裏是你如何創建的狀態項:
NSStatusItem* item = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
DragStatusView* dragView = [[DragStatusView alloc] initWithFrame:NSMakeRect(0, 0, 24, 24)];
[item setView:dragView];
[dragView release];
太棒了!但我如何處理點擊這個視圖並顯示菜單? – Oleg 2012-06-20 11:14:57
@Oleg是否能夠實現點擊處理並在此視圖上顯示菜單? – 2013-02-05 10:10:21
我添加按鈕。然後添加DragStatusView作爲子視圖。 \t'_titleButton = [[NSButton alloc] initWithFrame:NSMakeRect(0,-2,26,24)]; \t [_titleButton setBordered:NO]; \t [_titleButton setButtonType:NSMomentaryChangeButton]; \t [_titleButton setImagePosition:NSImageOnly]; \t [_titleButton setBezelStyle:NSThickerSquareBezelStyle]; \t [_titleButton setTarget:self]; \t \t \t \t [_titleButton setImage:[NSImage imageNamed:@「IconDefault.png」]]; \t \t [_titleButton setAction:@selector(showMenu :)];; \t \t \t self.view = [[ILDragStatusView alloc] initWithFrame:NSMakeRect(0,1,26,24)]; \t [self.view addSubview:_titleButton];' – Oleg 2013-02-06 11:58:48