我使用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。就像我不能在那些委托方法中设置属性值一样吗?
发布于 2013-05-23 18:29:19
上面的问题可以通过在SSDraDropView.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-21 19:16:58
添加
[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上拖动任何内容。
SSDragDropView *dragView = [SSDragDropView new];
NSLog(@"DragView Value is %@",[dragView textToDisplay]); https://stackoverflow.com/questions/16667868
复制相似问题