Objective c 如何从interfacebuilder访问窗口视图?
我正在尝试在xcode的接口生成器中为cocoa应用程序创建接口。我在Interface builder中创建的一个窗口的视图依赖于另一个视图中的数据,因此有必要从必须传递数据的类中向该视图发送消息。但是,我似乎找不到从nib文件的所有者处获取该视图对象的引用的方法。下面是我正在使用的大致代码:Objective c 如何从interfacebuilder访问窗口视图?,objective-c,xcode,cocoa,interface-builder,Objective C,Xcode,Cocoa,Interface Builder,我正在尝试在xcode的接口生成器中为cocoa应用程序创建接口。我在Interface builder中创建的一个窗口的视图依赖于另一个视图中的数据,因此有必要从必须传递数据的类中向该视图发送消息。但是,我似乎找不到从nib文件的所有者处获取该视图对象的引用的方法。下面是我正在使用的大致代码: controller = [[NSWindowController alloc] initWithWindowNibName:@"Somenibname"]; [[controller window]
controller = [[NSWindowController alloc] initWithWindowNibName:@"Somenibname"];
[[controller window] display];
theOtherView = [[[[controller window] contentView] subviews] objectAtIndex:1];
[theOtherView setObjectwhichneedstobemessaged:self];
[theOtherView sendAMessage:self];
此代码所在的对象从不接收消息。起初,我实际上认为contentview是interface builder中出现的视图,并试图像这样获取对它的引用
theOtherView=[[controller window]contentView]]
但这也不起作用。感谢阅读。听起来您可能需要更好地了解视图控制器结构如何与nib文件一起工作,如果没有更多的代码/细节,就很难确切地知道您要做什么,但解决问题的一个快速方法可能是使用NSNotification,而不是试图定位另一个视图并通过调用链发送消息
您可以登记处理接收视图中的通知并从启动视图发送通知(反之亦然,如果您需要两种方式)。
< P> > <代码> IBOutlet < /C> >并考虑直接从接口生成器链接您需要的视图。< /P> 例如,您的NSWindowController
子类可能有:
@interface MyWindowController : NSWindowController
{
/* can also use more specific classes if you need them, e.g. NSButton if it's really an NSButton */
IBOutlet NSView* firstViewIWant;
IBOutlet NSView* secondViewIWant;
}
. . .
@end
- (void)
windowDidLoad
{
[super windowDidLoad];
/* make sure the views were connected properly */
assert(nil != firstViewIWant);
assert(nil != secondViewIWant);
. . .
}
您的实现可能有:
@interface MyWindowController : NSWindowController
{
/* can also use more specific classes if you need them, e.g. NSButton if it's really an NSButton */
IBOutlet NSView* firstViewIWant;
IBOutlet NSView* secondViewIWant;
}
. . .
@end
- (void)
windowDidLoad
{
[super windowDidLoad];
/* make sure the views were connected properly */
assert(nil != firstViewIWant);
assert(nil != secondViewIWant);
. . .
}
然后在Interface Builder中,将“文件所有者”的这些输出连接到您需要的确切视图