我有一個NSImageView
子類,我用它來拖放文件到我的應用程序。設置委託給NSImageView的子類放置在Xib
爲此,我創建了NSImageView
的子類,並將NSImageView
拖到我的XIB上,然後將XIB NSImageView
對象的類設置爲我的自定義子類。
一切正常與拖放和我知道我有正確的文件拖動後。
的問題出現時,我想更新基礎上拖在文件上MainViewController一個文本框。
我創建了下面的子類和協議
#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
@protocol PDFDraggedIntoWell <NSObject>
@required
-(void)PDFDraggedIntoWellWithURL:(NSURL*) importedURL;
@end
@interface DragAndDropImageView : NSImageView
@property (strong) id <PDFDraggedIntoWell> delegate;
@end
然後在我的實現在子類中我嘗試撥打代表方法
-(void) finishedDragginInFileWithURL:(NSURL*) importedURL{
if(self.delegate != nil && [ self.delegate respondsToSelector: @selector(PDFDraggedIntoWellWithURL:)]) {
[self.delegate performSelector:@selector(PDFDraggedIntoWellWithURL:) withObject:importedURL];
}
}
我遇到的問題是您如何分配委託。從XIB NSImageView我MainviewController我連接了一個IBOutlet
@property (weak) IBOutlet DragAndDropImageView *draggedFileImageView;
而且我宣佈我的ViewController將收到的委託
@interface MyMainUIViewController()<PDFDraggedIntoWell>
用適當的方法來實現
-(void) PDFDraggedIntoWellWithURL:(NSURL *)importedURL{ }
現在我曾在各地嘗試將代表分配給自己(在viewDidLoad
- 由於視圖正在加載,所以不會被調用d在一個XIB ??),也在
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
但我所得到的是委託仍然是調試時零。
我在做什麼錯?謝謝您的幫助!!
什麼是定義爲('NSObject','NSWindowController','NSViewController'等的子類的'MyMainUIViewController')? – NSGod
它是一個NSViewController – Prasanth