我正在使用 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]);
但我每次都为空。是不是我不能在那些委托方法中设置属性值?