gpt4 book ai didi

macos - 如何在NSViewController中调用registerForDraggedTypes?

转载 作者:行者123 更新时间:2023-12-03 16:15:07 29 4
gpt4 key购买 nike

我有一个 MainViewController ,其中有一个 splitview 。接下来,我有两个 View Controller 来控制分割 View 中的每个 View 。我希望能够在这些 View 之一中拖放文件。

但我似乎无法让拖动工作?当将文件拖到 View 中时,文件上没有“加号”,并且放下它也不会执行任何操作。

我做错了什么?

首先是 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]];

接下来,我的代码用于处理 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];
}
}

最佳答案

我似乎没有修复它,因为没有人给我任何答案,我通过创建一个 NSView 子类并将其添加到我的 View Controller 中来修复它,如下所示。

- (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;
}

关于macos - 如何在NSViewController中调用registerForDraggedTypes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8154521/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com