Shel
Shel

Reputation: 136

How can I have alternate nib files for a given ViewController and change them on the fly?

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.

Upvotes: 4

Views: 1322

Answers (3)

Shel
Shel

Reputation: 136

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.

Upvotes: 0

NSResponder
NSResponder

Reputation: 16861

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.

Upvotes: 2

fearmint
fearmint

Reputation: 5334

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.

Upvotes: 0

Related Questions