如何使用NSOutlineVIew实现拖放 [英] How do i implement Drag and drop with NSOutlineVIew

查看:267
本文介绍了如何使用NSOutlineVIew实现拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个NSTableViews在屏幕上;我只想将一行从一个表拖到另一个表。我在这里和那里看到很多提示,但我没有看到一个完整的例子,我有点困惑。我看到了与苹果示例应用完全不同的示例 TableView playground 拖放outlineView

I have two NSTableViews on screen; I just want to drag a row from one table to the other table. I see lots of tips here and there but I do not see a complete example and I'm a bit confused. I saw examples that were totally different to Apples sample apps TableView playground and drag and drop outlineView.

我决定使用Apples方法,但现在已经卡住了。 TableView playground在其模型对象中实现这些方法。

I decided to use Apples method, but now im stuck. TableView playground implement these methods in their model object.

- (NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard
- (id)pasteboardPropertyListForType:(NSString *)type
- (NSPasteboardWritingOptions)writingOptionsForType:(NSString *)type pasteboard:(NSPasteboard *)pasteboard

我不明白如何设置这些。对于第一个方法,我返回一个数组 @com.mycompany.myapp.mypasteboardtype,如问题。

I dont understand how to set these up. For the 1st method i returned an array with string @"com.mycompany.myapp.mypasteboardtype"as suggested in this question.

我应该为第二种方法放什么?我的模型是一个自定义对象,它有许多字符串,数组和字典变量。我也不明白第三种方法。我希望有一些例子,我可以看到,做一个简单的拖动从一个表到另一个与自定义模型对象。

What should i put for the 2nd method? My model is a custom object that has a number of strings, Arrays, and dictionary variables. I also do not understand the 3rd method. I wish there was some example i could see that does a simple drag from one table to another with a custom model object.

编辑:我的实施基于以下回应:<$ p

My implementation based on response below

-(id)pasteboardPropertyListForType:(NSString *)type {
    return [NSKeyedArchiver archivedDataWithRootObject:self];
}

-(NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard {
    return [NSArray arrayWithObject:myDragType];
}
// Other methods that need to be implemented

-(id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type {
    return [NSKeyedUnarchiver unarchiveObjectWithData:propertyList];
}

+(NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard {
    return  [NSArray arrayWithObject:myDragType];
}

// And finally your object needs comply with NSCoder protocol.  These following 2 methods needs to go in the object model associated with a row.
-(void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:oneOfMyIvarsToEncode forKey:@"someKey"];
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        oneOfMyEndodedIvars = [aDecoder decodeObjectForKey:@"someKey"];
    }

    return self;
}


推荐答案


我应该为[ pasteboardPropertyListForType:]放置什么?我的模型是一个自定义对象,它有许多字符串,数组和字典变量。

What should i put for [pasteboardPropertyListForType:]? My model is a custom object that has a number of strings, Arrays, and dictionary variables.

如果是您自己的自定义类型,您可以放任何你想要的。真的 - 如果它是你发明的类型,你可以返回任何你想要的,只要它是一个有效的属性列表。您只需确保您的粘贴/放置代码(粘贴板)预期的内容相同。

If it's your own custom type that you made up, you can put whatever you want. Really—if it's your type that you've invented, you can return whatever you want here, as long as it's a valid property list. You just have to make sure that your paste/drop code (pasteboard reading) is expecting the same stuff.

如果您要支持内部拖动(在层次结构中重新排序和/或重定位),您应该至少有一个自定义类型标识对象,以便在接受下拉时可以查找相同的对象(为了移动它而不是复制它)。对于Core Data对象,您可以使用对象的标识URI

If you want to support internal drags (reordering and/or relocation within the hierarchy), you should have at least one custom type that identifies the object so that you can look up the same object when accepting the drop (in order to move it rather than duplicate it). For a Core Data object, you might use the absolute string of the object's identifying URI.

如果要支持拖动到其他应用程序,您应该至少有一个非自定义类型,这些其他应用程序将识别。你支持什么类型取决于你的模型代表什么:如果他们是图像(或创建他们的食谱,例如图层堆叠),你可以支持PNG,TIFF,JPEG等;如果他们是联系人,您可以支持vCard;如果它们是文本,您可以支持纯文本和/或RTF和/或HTML和/或WebArchive和/或Microsoft Word;等等。

If you want to support dragging to other applications, you should have at least one non-custom type that those other applications will recognize. What type(s) you support will depend on what your model represents: if they're images (or recipes for creating them, such as stacks of layers), you could support PNG, TIFF, JPEG, etc.; if they're contacts, you could support vCard; if they're text, you could support plain text and/or RTF and/or HTML and/or WebArchive and/or Microsoft Word; etc.

你返回一个数组,这个对象可以从 writableTypesForPasteboard:之后, pasteboardPropertyListForType:必须查看请求的类型并返回该类型的属性列表。

You return an array of the types that this object can be turned into from writableTypesForPasteboard:; afterward, pasteboardPropertyListForType: must look at what type it was asked for and return a property list of that type.

对于大多数外部格式, pasteboardPropertyListForType:必须返回一个数据对象。对于你自己的自定义格式,它通常是一个字典或一个字典数组,虽然,正如我所说,唯一真正的要求是它必须是某种plist,你的阅读代码必须能够理解它。 p>

For most external formats, pasteboardPropertyListForType: must return a data object. For your own custom formats, it's usually either a dictionary or an array of dictionaries, although, as I said, the only real requirements are that it must be a plist of some sort and your reading code must be able to understand it.


我也不明白[ writingOptionsForType:pasteboard:]。

您必须返回一个位掩码,指示何时以及如何将类型写入粘贴板。 可用选项已记录

You must return a bit mask indicating when and how you will write the type to the pasteboard. The available options are documented.

目前只有一个:您可以承诺数据。这意味着粘贴板不会立即询问您的数据;它将等待直到用户在某处粘贴或丢弃它(或者数据被任何应用程序以其他方式请求 - 例如,写得很差的下拉验证方法可以请求数据并在验证期间检查它,而不是丢弃接受)。只有这样,它会为该类型调用 pasteboardPropertyListForType:。 (这是一个每个类型的选项;你可以选择承诺一些类型,而不是其他类型。)

Currently, there's only one: You can promise the data. This means that the pasteboard will not immediately ask you for the data; it will wait until the user pastes or drops it somewhere (or the data is otherwise requested by any application—e.g., a poorly-written drop validation method could request the data and examine it during validation, rather than drop acceptance). Only then will it call pasteboardPropertyListForType: for that type. (And this is a per-type option; you can choose to promise some types but not others.)

Promising是伟大的计算昂贵的数据和/商店;例如压缩档案(计算)或大型图片(商店)。

Promising is great for data that is expensive to compute and/or store; for example, a compressed archive (compute) or a large image (store).

这篇关于如何使用NSOutlineVIew实现拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆