我正在尝试扩展NSImageView,以便将拖放责任委派给控制器.一切正常,编译器现在显示有关向类型为id的对象发送消息的警告.为了解决这个问题,我假设我只需要使用协议名称后缀ivar的类型.但是,由于无法找到协议的定义,因此失败了.
#import <Cocoa/Cocoa.h>
@interface DragDropImageView : NSImageView {
id <DragDropImageViewDelegate> _delegate;
}
@property (readwrite, retain) id <DragDropImageViewDelegate> delegate;
@end
@protocol DragDropImageViewDelegate
@optional
- (NSDragOperation)dragDropImageView:(DragDropImageView *)ddiv validateDrop:(id <NSDraggingInfo>)info;
- (BOOL)dragDropImageView:(DragDropImageView *)ddiv acceptDrop:(id <NSDraggingInfo>)info;
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender;
@end
Run Code Online (Sandbox Code Playgroud)
我可能会出错的任何指针?我敢肯定它一定很简单,但我对obj-c很新.
我正在尝试构建一个应用程序,允许用户将文件从Finder拖动到菜单栏图标进行处理.我的旅程取得了进展,但我似乎无法登顶这座山.我尝试了子类化NSView并实现拖动消息.
@interface CMDroppableView : NSView <NSMenuDelegate>
Run Code Online (Sandbox Code Playgroud)
我不仅要接受拖动操作,还要在用户单击图标时提供NSMenu.我已经设法让NSMenu正常显示,但拖动功能仍然难以捉摸.
我的理解是我需要注册我在这里完成的已接受的拖动类型:
-(void)awakeFromNib{
[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
}
Run Code Online (Sandbox Code Playgroud)
拖动邮件:
-(NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{
NSLog(@"Drag Enter");
return NSDragOperationCopy;
}
-(NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender{
return NSDragOperationCopy;
}
-(void)draggingExited:(id <NSDraggingInfo>)sender{
NSLog(@"Drag Exit");
}
-(BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender{
return YES;
}
-(BOOL)performDragOperation:(id <NSDraggingInfo>)sender{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
以下是设置自定义视图的代码:
statusItemView = [[CMDroppableView alloc] init];
[statusItemView retain];
[statusItemView setMenu: statusMenu];
[statusItem setView: statusItemView];
Run Code Online (Sandbox Code Playgroud)
依然没有.那么我哪里出错了?
谢谢!
我正在编写一个需要接受文件夹丢弃的自定义视图.条件是:只接受目录,因此当用户拖动文件时,不会发生任何事情.
我注册了我的观点:
[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
Run Code Online (Sandbox Code Playgroud)
并且已经实现了基本的拖动协议方法.用于测试目的:
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
NSLog("@Drag Entered");
return NSDragOperationCopy;
}
- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender
{
return NSDragOperationCopy;
}
- (void)draggingExited:(id<NSDraggingInfo>)sender
{
NSLog(@"Dragging Exited");
}
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender { return YES; }
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { return YES; }
Run Code Online (Sandbox Code Playgroud)
所以它的工作原理几乎正确:在视图上拖动时光标会出现加号.但是,如果该项目是常规文件,我想避免这种情况.
我可能需要使用NSFileManager(虽然我想知道是否有更简单的方法)一旦我得到拖动的路径,但问题是在哪里.我试图在draggingEntered:
返回NSDragOperationNone的方法中包含测试,但没有成功.我正在关注Apple文档的片段:
{
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
int numberOfFiles = [files count];
// Perform operation using the list of files
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
我应该在哪里实现此测试,因此如果拖动文件,光标保持不变?
cocoa drag-and-drop objective-c osx-snow-leopard nsdragginginfo
我正在尝试编写一些完整的准系统代码,我可以将一个简单的'dot.txt'文件拖到NSWindow上并读入数据(并没有比这更好的),但我能够找到的所有示例都使用图像和NSViews等.苹果公司的"拖拽文件内容"部分在其"Cocoa的拖放编程主题"文档中确认拖动到一个普通的NSWindow(而不是NSView等)是可能的,似乎正在讨论我到底是什么我试图做,但作为一个相对新手我仍然发现它的图像和框架混淆参考.
任何人都可以帮助我开始向我展示'registerForDraggedTypes'的位置,而不是把它放在说,'initWithFrame'或'initWithCoder'方法,以及注册哪些类型?一旦我得到窗口识别我的拖动,我可以担心其他'performDragOperation'和'draggingEntered'的东西.
谢谢 :-)
我是在一个NSView中拖放,但从draggingEntered:
未被调用过.代码:
#import <Cocoa/Cocoa.h>
@interface testViewDrag : NSView <NSDraggingDestination>
@end
@implementation testViewDrag
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self registerForDraggedTypes:[NSImage imagePasteboardTypes]];
NSLog(@"initWithFrame");
}
return self;
}
-(NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
NSLog(@"draggingEntered");
return NSDragOperationEvery;
}
-(NSDragOperation) draggingUpdated:(id<NSDraggingInfo>)sender
{
NSLog(@"draggingUpdated");
return NSDragOperationEvery;
}
@end
Run Code Online (Sandbox Code Playgroud)
在界面构建器中,我将一个subView(类设置为testViewDrag)添加到主窗口.在日志中我可以看到initWithFrame
日志,但是当我拖动时,日志中不会显示任何内容.
我错过了什么?
我在主xib(MainMenu.xib)中有一个小窗口,带有一个用于OS X应用程序的NSImageView控件.这个视图控件有一个NSImageView子类,它应该接受用户带来的文件(拖放).由于我没有使用Objective-C开发Mac应用程序的经验,所以我一直在搜索,查看Apple的一些示例项目,并得到一些想法.好吧,为了简短起见,我刚刚复制了这里发布的代码.有用.太棒了......以下是简洁版.
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{
return NSDragOperationCopy;
}
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender{
}
- (void)draggingExited:(id <NSDraggingInfo>)sender{
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender{
return YES;
}
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSPasteboard *pboard = [sender draggingPasteboard];
if ([[pboard types] containsObject:NSURLPboardType]) {
NSURL *fileURL = [NSURL URLFromPasteboard:pboard];
NSLog(@"Path: %@", [self convertPath:fileURL]); // <== That's just what I need
}
return YES;
}
- (NSString *)convertPath:(NSURL *)url {
return url.path;
}
Run Code Online (Sandbox Code Playgroud)
目前,下拉框仅一次获取一个文件路径,而不管用户拖放到下拉框中的文件数量.所以我想知道的是如何让应用程序读取用户带来的所有多个文件.
谢谢,
macos objective-c nspasteboard osx-mountain-lion nsdragginginfo
我想在我的Mac应用程序中实现一些"简单"的拖放操作.我在视图中拖动并相应地在我的nib文件中输入"MyView".但是我的控制台中没有得到任何响应(我尝试在触发任何协议方法时记录消息)
我有这样的NSView子类:
@interface MyView : NSView <NSDraggingDestination>{
NSImageView* imageView;
}
Run Code Online (Sandbox Code Playgroud)
并像这样实现它:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self registerForDraggedTypes:[NSImage imagePasteboardTypes]];
NSRect rect = NSMakeRect(150, 0, 400, 300);
imageView = [[NSImageView alloc] initWithFrame:rect];
[imageView setImageScaling:NSScaleNone];
[imageView setImage:[NSImage imageNamed:@"test.png"]];
[self addSubview:imageView];
}
return self;
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{
NSLog(@"entered");
return NSDragOperationCopy;
}
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender{
return NSDragOperationCopy;
}
- (void)draggingExited:(id <NSDraggingInfo>)sender{
NSLog(@"exited");
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender{
NSLog(@"som");
return YES;
}
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSPasteboard *pboard = [sender draggingPasteboard]; …
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中实现了拖放功能.所有功能都很好但是当我们将图像拖入时NSTableView
,
- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender
Run Code Online (Sandbox Code Playgroud)
方法没有被调用.
任何人都可以告诉我为什么不调用这种方法的原因?
即使我实施这个也...
- (BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender
Run Code Online (Sandbox Code Playgroud)