2012-09-16 54 views
1

我有一個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 

但我所得到的是委託仍然是調試時零。

我在做什麼錯?謝謝您的幫助!!

+0

什麼是定義爲('NSObject','NSWindowController','NSViewController'等的子類的'MyMainUIViewController')? – NSGod

+0

它是一個NSViewController – Prasanth

回答

0

如果您使用的是xibx,則調用initWithCoder方法進行初始化。在那裏設置你的代表。

- (id)initWithCoder:(NSCoder *)aDecoder { 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 
     myView.delegate = self; 
    } 
    return self; 
} 

或者通過將wile按住ctrl從文件所有者拖到您的視圖來通過界面生成器設置委託。像這樣:
enter image description here

+0

感謝Pfitz,initwithCoder方法在哪裏?在我的主視圖控制器?如果它進入子類NSImageView實現,它將如何知道代理需要引用哪個類?此外,我的IB沒有將代表顯示爲可用網點之一?再次感謝。 – Prasanth

0

我能夠添加委託添加到awakefromnib(在mymainviewcontroller)方法和事情現在工作正常。謝謝