How do I set my nib File's Owner to an object in another nib file?

匆匆过客 提交于 2019-12-23 23:59:26

问题


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

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.

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

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.)

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?


回答1:


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.


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];

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


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

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



来源:https://stackoverflow.com/questions/6013352/how-do-i-set-my-nib-files-owner-to-an-object-in-another-nib-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!