2012-07-08 44 views
4

此項目的靈感來源於Droplr和CloudApp mac菜單欄應用程序。我一直試圖執行解釋爲here的代碼。Mac應用程序 - 將文件拖放到菜單欄應用程序

但是,當我執行代碼時,菜單欄圖像消失。這裏是我的代碼來創建的狀態項:

- (id)init { 
self = [super init]; 
if (self != nil) { 
    // Install status item into the menu bar 
    NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:STATUS_ITEM_VIEW_WIDTH]; 
    _statusItemView = [[StatusItemView alloc] initWithStatusItem:statusItem]; 
    _statusItemView.image = [NSImage imageNamed:@"Status"]; 
    _statusItemView.alternateImage = [NSImage imageNamed:@"StatusHighlighted"]; 
    _statusItemView.action = @selector(togglePanel:); 
    StatusItemView* dragView = [[StatusItemView alloc] initWithFrame:NSMakeRect(0, 0, 24, 24)]; 
    [statusItem setView:dragView]; 
    [dragView release]; 
} 
return self; 
} 

這是我的看法文件:

#import "StatusItemView.h" 
@implementation StatusItemView 
@synthesize statusItem = _statusItem; 
@synthesize image = _image; 
@synthesize alternateImage = _alternateImage; 
@synthesize isHighlighted = _isHighlighted; 
@synthesize action = _action; 
@synthesize target = _target; 
#pragma mark - 
- (id)initWithStatusItem:(NSStatusItem *)statusItem { 
    CGFloat itemWidth = [statusItem length]; 
    CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness]; 
    NSRect itemRect = NSMakeRect(0.0, 0.0, itemWidth, itemHeight); 
    self = [super initWithFrame:itemRect]; 
    if (self != nil) { 
    _statusItem = statusItem; 
    _statusItem.view = self; 
    } 
return self; 
} 

#pragma mark - 
- (id)initWithFrame:(NSRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
    //register for drags 
    [self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]]; 
    } 
    return self; 
} 
//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; 
} 

#pragma mark - 

- (void)drawRect:(NSRect)dirtyRect { 
    [self.statusItem drawStatusBarBackgroundInRect:dirtyRect withHighlight:self.isHighlighted]; 
    NSImage *icon = self.isHighlighted ? self.alternateImage : self.image; 
    NSSize iconSize = [icon size]; 
    NSRect bounds = self.bounds; 
    CGFloat iconX = roundf((NSWidth(bounds) - iconSize.width)/2); 
    CGFloat iconY = roundf((NSHeight(bounds) - iconSize.height)/2); 
    NSPoint iconPoint = NSMakePoint(iconX, iconY); 
    [icon compositeToPoint:iconPoint operation:NSCompositeSourceOver]; 
} 

#pragma mark - 
#pragma mark Mouse tracking 

- (void)mouseDown:(NSEvent *)theEvent { 
    [NSApp sendAction:self.action to:self.target from:self]; 
} 

#pragma mark - 
#pragma mark Accessors 
- (void)setHighlighted:(BOOL)newFlag { 
    if (_isHighlighted == newFlag) return; 
    _isHighlighted = newFlag; 
    [self setNeedsDisplay:YES]; 
} 

#pragma mark - 
- (void)setImage:(NSImage *)newImage { 
    if (_image != newImage) { 
    _image = newImage; 
    [self setNeedsDisplay:YES]; 
    } 
} 

- (void)setAlternateImage:(NSImage *)newImage { 
    if (_alternateImage != newImage) { 
    _alternateImage = newImage; 
    if (self.isHighlighted) { 
     [self setNeedsDisplay:YES]; 
    } 
    } 
} 

#pragma mark - 
- (NSRect)globalRect { 
    NSRect frame = [self frame]; 
    frame.origin = [self.window convertBaseToScreen:frame.origin]; 
    return frame; 
} 

@end 

謝謝大家!

回答

0

我知道這是一個老問題,但也許這可能幫助:

嘗試設置類的NSStatusItem *statusItem作爲@propery。如果你有ARC,那麼在初始化函數完成之後,菜單欄可能會被破壞。