设置委托给NSImageView的子类放置在Xib
我有一个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
但我所得到的是委托仍然是调试时零。
我在做什么错?谢谢您的帮助!!
如果您使用的是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从文件所有者拖到您的视图来通过界面生成器设置委托。像这样:
感谢Pfitz,initwithCoder方法在哪里?在我的主视图控制器?如果它进入子类NSImageView实现,它将如何知道代理需要引用哪个类?此外,我的IB没有将代表显示为可用网点之一?再次感谢。 – Prasanth
我能够添加委托添加到awakefromnib(在mymainviewcontroller)方法和事情现在工作正常。谢谢
什么是定义为('NSObject','NSWindowController','NSViewController'等的子类的'MyMainUIViewController')? – NSGod
它是一个NSViewController – Prasanth