【问题标题】:Drag Drop delegate for NSView could not set an attributeNSView 的拖放委托无法设置属性
【发布时间】:2013-05-21 10:32:51
【问题描述】:

我正在使用 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];
       }

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

SSDragDropView *dragView = [SSDragDropView new];
    NSLog(@"DragView Value is %@",[dragView textToDisplay]); 

但我每次都得到空值。是不是我不能在那些委托方法中设置属性值?

【问题讨论】:

    标签: cocoa drag-and-drop nsview nsdragginginfo


    【解决方案1】:

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

    #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
    }
    

    :)

    【讨论】:

      【解决方案2】:

      添加

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

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

      SSDragDropView *dragView = [SSDragDropView new];
          NSLog(@"DragView Value is %@",[dragView textToDisplay]); 
      

      【讨论】:

      • 嗨,帕拉格,我已经修改了委托:draggingEntered.. 当我访问“textToDisplay”属性时,还在另一个类中添加了提到的代码,但我仍然得到空值。
      • 嗨,帕拉格,文本显示在 SSDragDropView 中 - 在委托中--(void)concludeDragOperation:(id )sender{ ...... ....这里 NSString *textDataFile = [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil]; [自我 setTextToDisplay:textDataFile]; NSLog(@"textData is %@",textDataFile);// 这是显示的 } 但我的要求是通过创建 SSDragDropView 的对象来访问其他类中的文本字符串(比如在 AppDelegate 中)。目前该值返回 null。
      • 我在 xib 文件上添加了一个按钮,并在 AppDelegate 中添加了一个操作 --(IBAction)data:(id)sender { SSDragDropView *dragView = [[SSDragDropView alloc] init]; [dragView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]]; NSLog(@"DragView Value is %@",[dragView textToDisplay]);// 返回null }
      • 它将返回 nil,因为没有要显示的文本。
      猜你喜欢
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      相关资源
      最近更新 更多