質問

NSCOLLECTIONVIEWをサブクラス化し、Finderからドラッグされたファイルを受信しようとしています。私は受け取っています draggingEntered: 適切な価値を返しますが、私は決して受け取っていません prepareForDragOperation: (その後のその後の方法のいずれか)。私がここに欠けていることが明らかなことはありますか?

コード:

- (void)awakeFromNib
{
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSLog(@"entered"); //Happens
    NSPasteboard *pboard;
    NSDragOperation sourceDragMask;

    sourceDragMask = [sender draggingSourceOperationMask];
    pboard = [sender draggingPasteboard];

    if ([[pboard types] containsObject:NSFilenamesPboardType])
    {
        NSLog(@"copy"); //Happens
        return NSDragOperationCopy;
    }

    return NSDragOperationNone;
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    NSLog(@"prepare"); //Never happens
    return YES;
}
役に立ちましたか?

解決

これはかなり遅れていますが、私は問題を見つけました:

nscollectionViewは、以下の互換性のない実装を静かに提供します

-(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender

...そして、Appleはこれを文書化していません。 Draggingentered Methodを再侵入するためにその方法を実装するだけで、すべてが正常に機能します。

-(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender
{
    return [self draggingEntered:sender];
}

(私はこのカスタム実装が提供する「魔法」の説明を見つけたいと思っていました。それも文書化されていないので(ありがとう、Apple!)。 CollectionView?)。

更新:特別な魔法は、NSCOLLECTIONVIEWのデリゲートオブジェクトの中にあるようです。何らかの理由で、Xcode4は私には代表者がいないと主張していましたが、それを構築して実行しました。カスタム /セミドキュメントドラッグ /ドロップメソッドをすべてチェックしてください。

(または、私が上記で説明し、カスタム動作をオーバーライドし、機能するものを実装し、理解できるものを実装するだけです)

他のヒント

これらの委任方法をから試してみることができます nscollectionViewDelegateプロトコル

- (NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id <NSDraggingInfo> )draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation;
- (BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id <NSDraggingInfo> )draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation;

- (BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event;
- (NSImage *)collectionView:(NSCollectionView *)collectionView draggingImageForItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent *)event offset:(NSPointPointer)dragImageOffset;
- (NSArray *)collectionView:(NSCollectionView *)collectionView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropURL forDraggedItemsAtIndexes:(NSIndexSet *)indexes;
- (BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard;

特に最初の2つの方法。

私はしばらく前にこれを経験しました。それは私にとって直感に反しているように見えましたが、私がそれを動作させることができる唯一の方法は、関連するスクロールビューをドロップターゲットとしてセットアップすることでした。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top