How to call registerForDraggedTypes in NSViewController?

I have a MainViewController with splitview in it. Then I have two view managers to manage each of the views in splitview. I want to be able to drag and drop a file in one of these views.

But I can’t get the drag to work? There is no "plus sign" in the file when you drag it into the view and resetting it also does nothing.

What am I doing wrong?

First here is MainViewController.m

fileViewController = [[FileViewController alloc] initWithNibName:@"FileViewController" bundle:nil]; terminalViewController = [[TerminalViewController alloc] initWithNibName:@"TerminalViewController" bundle:nil]; [splitView replaceSubview:[[splitView subviews] objectAtIndex:0] with:[fileViewController view]]; [splitView replaceSubview:[[splitView subviews] objectAtIndex:1] with:[terminalViewController view]]; 

Then my drag and drop code in FileViewController

 @dynamic isHighlighted; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { NSLog(@"registering"); [self.view registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]]; } return self; } - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender { NSLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd)); NSPasteboard *pboard = [sender draggingPasteboard]; if ([[pboard types] containsObject:NSFilenamesPboardType]) { NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType]; for (NSString *path in paths) { NSError *error = nil; NSString *utiType = [[NSWorkspace sharedWorkspace] typeOfFile:path error:&error]; if (![[NSWorkspace sharedWorkspace] type:utiType conformsToType:(id)kUTTypeFolder]) { [self setHighlighted:NO]; return NSDragOperationNone; } } } [self setHighlighted:YES]; return NSDragOperationEvery; } - (void)draggingExited:(id <NSDraggingInfo>)sender { [self setHighlighted:NO]; } - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender { return YES; } - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender { [self setHighlighted:NO]; return YES; } - (void)concludeDragOperation:(id )sender { [self.view setNeedsDisplay:YES]; } // end concludeDragOperation - (BOOL)isHighlighted { return isHighlighted; } - (void)setHighlighted:(BOOL)value { isHighlighted = value; [self.view setNeedsDisplay:YES]; } - (void)drawRect:(NSRect)frame { [self.view drawRect:frame]; if (isHighlighted) { [NSBezierPath setDefaultLineWidth:6.0]; [[NSColor keyboardFocusIndicatorColor] set]; [NSBezierPath strokeRect:frame]; } } 
+4
source share
2 answers

I didn't seem to fix this, and since no one gave me any answers, I fixed it by subclassing NSView and adding it to my view manager like this.

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { draggable = [[DragView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)]; [[self view] addSubview:draggable]; } return self; } 
+2
source

I have never programmed OS X, but the problem is that since you are registering self.view for drag and drop, your self.view should handle the drag and drop operations, not your view controller. So, you're right in the NSView subclass before DragView , and you have to pass the drag and drop operations back to the view controller.

In addition, I believe that - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender should be implemented.

0
source

Source: https://habr.com/ru/post/1381516/


All Articles