首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSView的拖放委托无法设置属性

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

Stack Overflow用户
提问于 2013-05-21 18:32:51
回答 2查看 478关注 0票数 0

我使用NSView委托来读取拖动的excel值。为此,我创建了NSView的子类。我的代码是这样的-

代码语言:javascript
复制
@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];
       }

我将textDataFile值设置为该类的字符串属性。现在,我在其他类中使用SSDragDropView属性值,比如-

代码语言:javascript
复制
SSDragDropView *dragView = [SSDragDropView new];
    NSLog(@"DragView Value is %@",[dragView textToDisplay]); 

但是我每次都会得到null。就像我不能在那些委托方法中设置属性值一样吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-23 18:29:19

上面的问题可以通过在SSDraDropView.h类中声明一个全局变量来解决。

代码语言:javascript
复制
#import <Cocoa/Cocoa.h>
NSString *myTextToDisplay;
@interface SSDragDropView : NSView
{

可以在所需的委托方法中设置相同的值

代码语言:javascript
复制
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender {

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

:)

票数 0
EN

Stack Overflow用户

发布于 2013-05-21 19:16:58

添加

代码语言:javascript
复制
[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;
}  

下面的代码将打印为空,因为您没有在NSView上拖动任何内容。

代码语言:javascript
复制
SSDragDropView *dragView = [SSDragDropView new];
    NSLog(@"DragView Value is %@",[dragView textToDisplay]); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16667868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档