0

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]); 

しかし、私は毎回nullになっています。それらのデリゲート メソッドで属性値を設定できないようなものですか?

4

2 に答える 2

0

上記の問題は、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
}

:)

于 2013-05-23T10:29:19.313 に答える