如何将我的nib文件的所有者设置为另一个nib文件中的对象? [英] How do I set my nib File's Owner to an object in another nib file?

查看:187
本文介绍了如何将我的nib文件的所有者设置为另一个nib文件中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cocoa新手,无法将我的nib文件分割成多个nib文件。

I'm new to Cocoa and am having trouble splitting my nib file into multiple nib files.

我的 MainMenu.nib 包含 Tracker Controller 对象,它是 NSObject 的子类。它有一个输出到显示跟踪器菜单项在我的主菜单。

My MainMenu.nib contains a Tracker Controller object, which is a subclass of NSObject. It has an outlet to a Show Tracker menu item in my main menu.

我的 TrackerWindow.nib 将文件的所有者类设置为 TrackerController ,并且具有到该窗口和该nib文件中的视图的插口。

My TrackerWindow.nib has the File's Owner class set to TrackerController, and has outlets to the window and view in that nib file.

我不知道如何使第二个nib的文件的所有者成为第一个nib中的实例化 TrackerController 的代理。 (我相信我需要在第一个nib中的 TrackerController 实例,以便我可以使用IB设置菜单项插口。)

I'm not sure how to make the File's Owner of the second nib be a proxy for the instantiated TrackerController in the first nib. (I believe I need the TrackerController instance in the first nib so that I can use IB to set the menu item outlet.)

我做错了吗?如果是这样,我如何使用IB设置多个nib文件中同一对象的插座?如果没有,我如何让第二个nib点的文件的所有者指向 TrackerController 我已经在第一个nib中实例化了

Am I doing it wrong? If so, how can I use IB to set outlets for the same object in multiple nib files? If not, how can I make the File's Owner of the second nib point to the TrackerController I've already instantiated in the first nib?

推荐答案

这是从iOS开发者的角度来编写的(使用视图控制器)。我不知道Mac OS X有什么不同,但不应该难以移植这些想法。

This is written from the perspective of an iOS developer (using view controllers). I'm not sure how Mac OS X differs but it shouldn't be difficult to transplant the ideas.

设置一个nib文件的所有者的最简单的方法是将它作为一个参数提供给 loadNibNamed:owner:options:

The simplest way to set the File's Owner of a nib is to provide it as an argument to loadNibNamed:owner:options::

[[NSBundle mainBundle] loadNibNamed:@"Tracker" owner:trackerController options:optionsDict];

上面的代码段假设 trackerController UIViewController的实例。

The snippet above assumes that trackerController is an instance of UIViewController. If it isn't, use the following solution instead.

而不是使用 initWithNibName: bundle:,创建一个 TrackerViewController 如下(其中 trackerController TrackerController对象):

Instead of using initWithNibName:bundle:, create a TrackerViewController as follows (where trackerController is a reference to the existing TrackerController object):

NSDictionary *proxyDict = [NSDictionary dictionaryWithObject:trackerController forKey:@"trackerController"];
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:proxyDict forKey:UINibExternalObjects];
TrackerViewController *trackerViewController = [[[TrackerViewController alloc] init] autorelease];
[[NSBundle mainBundle] loadNibNamed:@"Tracker" owner:trackerViewController options:optionsDict];
// Display trackerViewController

创建一个标识符为 trackerController ,并将您的插座/操作连接到此对象。

Create an External Object with an Identifier of trackerController in Tracker.nib and connect your outlets/actions to this object.

这篇关于如何将我的nib文件的所有者设置为另一个nib文件中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆