0

I am using NSView delegate to read the dragged excel values. For this I have subclassed NSView. My code is like-

@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];
       }

I am setting the textDataFile value to a string attribute of that class. Now I am using SSDragDropView attribute value in some other class like-

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

But I am getting null each time. Is it like I can not set an attribute value in those delegate methods?

Jay
  • 6,572
  • 3
  • 37
  • 65
triandicAnt
  • 1,328
  • 2
  • 15
  • 40

2 Answers2

0

Add

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

Below code will print nil because you are not dragging anything on NSView.

SSDragDropView *dragView = [SSDragDropView new];
    NSLog(@"DragView Value is %@",[dragView textToDisplay]); 
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • Hi Parag, I have modified the delegate : draggingEntered.. also added the mentioned code in another class when I was accessing the "textToDisplay" attribute, but still I am getting null. – triandicAnt May 21 '13 at 11:50
  • Take a look at (sample project) [https://www.dropbox.com/s/6r1a7di07ryi54b/Untitled.zip?v=1mci] – Parag Bafna May 21 '13 at 11:56
  • Hi Parag, The text is getting displayed in that SSDragDropView - in th e delegate--(void)concludeDragOperation:(id )sender{ ...............sth here NSString *textDataFile = [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil]; [self setTextToDisplay:textDataFile]; NSLog(@"textData is %@",textDataFile);// This is displayed } But My requirement is accessing that textString in some other class(say in AppDelegate) by creating an object of SSDragDropView. Currently that value is returning null. – triandicAnt May 23 '13 at 06:06
  • I added a button on the xib file and an action in AppDelegate --(IBAction)data:(id)sender { SSDragDropView *dragView = [[SSDragDropView alloc] init]; [dragView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]]; NSLog(@"DragView Value is %@",[dragView textToDisplay]);// returns null } – triandicAnt May 23 '13 at 06:11
  • It will return nil because there is no text to display. – Parag Bafna May 23 '13 at 06:14
  • How can I get the string value in other class ? – triandicAnt May 23 '13 at 06:17
  • add another property in your SSDragDropView class. property(assign) id delegate; Now [dragView setDelegate:self]; from concludeDragOperation method call one method [delegate getOutput: textDataFile]; Implement getOutput in your AppDelegate class. – Parag Bafna May 23 '13 at 06:17
  • I have added @property(assign) id delegate; and synthesized it. In SSDragDropView -(void)concludeDragOperation:(id )sender{ ----//sth here [self setTextToDisplay:textDataFile]; NSLog(@"textData is %@",textDataFile); [delegate getOutput: textDataFile]; } -(void)getOutput:(NSString*)str{ [delegate setTextToDisplay:str]; } And in AppDelegate I am using like- [dragView setDelegate:self]; [dragView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]]; NSLog(@"Val %@",[[dragView delegate] textToDisplay]);..Am I missing something? – triandicAnt May 23 '13 at 08:05
  • -(void)getOutput:(NSString*)output {NSLog(@"%@", output);} Implement this in your appdelegate – Parag Bafna May 23 '13 at 08:09
  • still not getting :( Can u plz make changes to your dropbox project.[dropbox.com/s/6r1a7di07ryi54b/Untitled.zip?v=1mci] – triandicAnt May 23 '13 at 08:19
0

The above problem can be resolved just by declaring a global variable in your SSDragDropView.h class.

#import <Cocoa/Cocoa.h>
NSString *myTextToDisplay;
@interface SSDragDropView : NSView
{

The same can be set inside the desired delegate method

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

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

:)