Cocoa NSView的拖放委托无法设置属性

Cocoa NSView的拖放委托无法设置属性,cocoa,drag-and-drop,nsview,nsdragginginfo,Cocoa,Drag And Drop,Nsview,Nsdragginginfo,我正在使用NSView委托读取拖动的excel值。为此,我将NSView子类化。我的代码是- @interface SSDragDropView : NSView { NSString *textToDisplay; } @property(nonatomic,retain) NSString *textToDisplay; // setters/getters @synthesize textToDisplay;// setters/gette

我正在使用NSView委托读取拖动的excel值。为此,我将NSView子类化。我的代码是-

@interface SSDragDropView : NSView
    {
        NSString *textToDisplay;
    }
    @property(nonatomic,retain) NSString *textToDisplay; // setters/getters

    @synthesize textToDisplay;// setters/getters

    @implementation SSDragDropView
    - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{
        [self setNeedsDisplay: YES];
        return NSDragOperationGeneric;
    }

    - (void)draggingExited:(id <NSDraggingInfo>)sender{
        [self setNeedsDisplay: YES];
    }

    - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
        [self setNeedsDisplay: YES];
        return YES;
    }


   - (BOOL)performDragOperation:(id < NSDraggingInfo >)sender {
        NSArray *draggedFilenames = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
        if ([[[draggedFilenames objectAtIndex:0] pathExtension] isEqual:@"xls"]){
            return YES;
        } else {
            return NO;
        }
    }

    - (void)concludeDragOperation:(id <NSDraggingInfo>)sender{
        NSArray *draggedFilenames = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
        NSURL *url =   [NSURL fileURLWithPath:[draggedFilenames objectAtIndex:0]];
       NSString *textDataFile = [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil]; //This text is the original excel text and its getting displayed.
    [self setTextToDisplay:textDataFile];
       }
但我每次都会变空。是否我无法在这些委托方法中设置属性值?

添加

[dragView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];  

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{

    NSPasteboard *pboard = [sender draggingPasteboard];
    NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
    NSLog(@"%@",paths);
    [self setNeedsDisplay: YES];
    return NSDragOperationGeneric;
}  

只需在SSDragDropView.h类中声明一个全局变量,就可以解决上述问题

#import <Cocoa/Cocoa.h>
NSString *myTextToDisplay;
@interface SSDragDropView : NSView
{
#导入
NSString*myTextToDisplay;
@接口SSDragDropView:NSView
{
也可以在所需的委托方法中设置

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender {

// .... //Your Code
NSString *textDataFile = [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
myTextToDisplay = textDataFile;
// .... //Your Code
}
-(void)结束删除操作:(id)发件人{
//..//您的代码
NSString*textDataFile=[NSString stringWithContentsOfURL:url usedEncoding:nil错误:nil];
myTextToDisplay=textDataFile;
//..//您的代码
}

:)

Hi Parag,我已经修改了委托:Draggineterd.。在访问“textToDisplay”属性时,我还在另一个类中添加了提到的代码,但仍然为空。查看(示例项目)[Hi Parag,文本显示在SSDragDropView中-在委托中--(void)ConclutedDragOperation:(id)发送方{………..此处为NSString*textDataFile=[NSString stringWithContentsOfURL:url usedEncoding:nil错误:nil];[self-setTextToDisplay:textDataFile];NSLog(@“textData is%@”,textDataFile);//这是显示的}但我的要求是在其他类中访问该文本字符串(比如在AppDelegate中)通过创建SSDragDropView的对象。当前该值返回null。我在xib文件上添加了一个按钮,并在AppDelegate--(iAction)数据中添加了一个操作:(id)发送方{SSDragDropView*dragView=[[SSDragDropView alloc]init];[dragView RegisterForDragedTypes:[NSArrayWithObjects:NSFileNamespoardType,nil];NSLog(@“DragView值为%@,[DragView textToDisplay]);//返回null}由于没有要显示的文本,它将返回nil。
#import <Cocoa/Cocoa.h>
NSString *myTextToDisplay;
@interface SSDragDropView : NSView
{
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender {

// .... //Your Code
NSString *textDataFile = [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
myTextToDisplay = textDataFile;
// .... //Your Code
}