我终于开始测试它,它完美的工作,所以你的代码肯定有问题。
这里有一个自定义视图,允许拖动:
@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