views:

481

answers:

3

I have a ViewController which controls a view that needs a different layout in landscape mode than in portrait mode. I made two .xib files that are semantically identical but have different layouts. I know there is a hack to display a second ViewController modally when in landscape mode, but I'd really rather just re-initialize the view controller with the other nib file when switching orientations.

I have it half working. When I turn the device while looking at the (tab bar) view in question, it disappears (The page, not the device!). But when I switch to another tab and come back to the view in question, it displays as I want it to.

How can I switch nibs, while looking at the view I'm going to re-layout, and not have it disappear?

Right now I'm doing the orientation sensing in -didRotateFromInterfaceOrientation: and also in -viewDidAppear:. I set self.view = nil and call initWithNibName:bundle:. Can this be made to work in a reasonable way or do I have to resort to the modal hack.

A: 

Nope. It's not supported, I think Apple has said it might come in the future, but for now to use the auto-resizing masks and layout info.

Also: You can't call nil on yourself, and then keep running the code of your controller, because it ends.

JoePasq
Thanks. The view controller doesn't set itself to nil, it sets its view to nil, and that is the one part of this that does seem to have the right effect. Also, I need a layout that auto-resizing masks can't do, so if this won't work I'll just have to display a modal view controller.
Shel
Hm... yeah sounds like another separate but connected view will do the trick. Now there's a method to present modal views in landscape mode if that helps.
JoePasq
+1  A: 

Don't forget that views can have subviews, which you can hide or show at will. In your case, I'd put layouts for both orientations in the same nib, and switch them when you get the -didRotate.. messages.

NSResponder
OK, nice solution! Plus, it took about 5 minutes to implement.I am not wild about having all that stuff in one NIB, but it is a lot more straightforward than the alternatives appear to have been.
Shel
It works great for the display aspect of things, but I am not so sure about hooking up all the UI elements to the view controller, which there are duplicates in each view (the portrait one and the landscape one).
Shel
A: 

Here's the first thing I've found that works (short of the known modal display hack): since all this is happening inside a tab bar controller, you just grab a mutable copy of the viewControllers property of the tab bar controller, replace the desired view controller in that array with a new one initialized from the NIB, and then put an immutable copy of the modified array back into the tab bar controller's viewControllers property. It displays right and preserves all the nib hookups. It should be easy to cache a copy of the alternate viewcontrollers.

Shel