objective-c – Mac应用程序 – 将文件拖放到菜单栏应用程序中

objective-c – Mac应用程序 – 将文件拖放到菜单栏应用程序中,第1张

概述我对这个项目的灵感来自Droplr和CloudApp mac菜单栏应用程序.我一直试图实现 here解释的代码. 但是,当我实现代码时,菜单栏图像消失了.这是我创建状态项的代码: - (id)init {self = [super init];if (self != nil) { // Install status item into the menu bar NSStatusItem 我对这个项目的灵感来自Droplr和CloudApp mac菜单栏应用程序.我一直试图实现 here解释的代码.

但是,当我实现代码时,菜单栏图像消失了.这是我创建状态项的代码:

- (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,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

感谢大家!

解决方法 我知道这是一个老问题,但也许这可能会有所帮助:

尝试将NsstatusItem * statusItem设置为类的@propery.如果你有ARC,那么在init函数完成后,菜单栏可能会被摧毁.

总结

以上是内存溢出为你收集整理的objective-c – Mac应用程序 – 将文件拖放到菜单栏应用程序中全部内容,希望文章能够帮你解决objective-c – Mac应用程序 – 将文件拖放到菜单栏应用程序中所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存