views:

549

answers:

2

I have a UIViewController that I want to load from a NIB that has a proxy (placeholder) object defined in it. The first time I load it, I go through this rigamarole:

MyViewController *screen = [[MyViewController alloc] init];

NSDictionary *proxyDict = [NSDictionary dictionaryWithObject:myObject forKey:@"MyProxy"];
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:proxyDict forKey:UINibExternalObjects];
[[NSBundle mainBundle] loadNibNamed:@"MyViewController" owner:screen options:optionsDict];

So this sets up the proxy object declared as "MyProxy" in the NIB file to point to the object myObject that already exists. This much works.

Now, if I go to a different screen and trigger a low memory warning, it unloads the view. when I go back to that screen, it does the automatic reloading of the view, which has no room for a manually-defined options dictionary for setting up proxy objects, and then it crashes trying to find an object to link "MyProxy" to.

How can I make this work?

+1  A: 

How about setting up the proxy object in -loadView or -viewDidLoad. Those will be called every time the view is loaded/reloaded (along with -viewDidUnload, after a low-memory condition).

Darren
The object has to be in the NIB file because there are IBActions linked to it.
Ed Marty
From the docs: "If you specify nil for the nibName parameter and do not override the loadView method in your custom subclass, the default view controller behavior is to look for a nib file whose name (without the .nib extension) matches the name of your view controller class" I'd guess the only way to do what you're trying to do is implement -loadView and load the nib yourself.
Darren
You would think that since proxy objects are supported in interface builder, they would also be backed up by some code that actually makes them work.
Ed Marty
If you want UIViewController to load your nib automatically it won't be able to use your proxy objects because it doesn't know about them. UIViewController does give you the opportunity to load the nib manually, so that's what you'll have to do. You initially load the nib manually, but you don't do it for subsequent re-loads.
Darren
Yes, I know all that. That's exactly the problem I'm trying to solve, thanks.
Ed Marty
A: 

Never found a solution for this, submitted a feature request, haven't heard back.

Ed Marty
So should we stay away from custom proxy objects?
Meltemi
Well, the way I got around it was by not having a view controller at all, and having my custom proxy object act as the view's owner
Ed Marty