So I followed the following tutorial on creating custom views associated with device orientations:
To summarize the article, the author is able to generate custom views for landscape and portrait orientations by pointing to a new top level view in an XIB file upon detecting a device rotation.
Therefore, I took a complicated view hierarchy (many buttons, sliders, views within views), duplicated everything at the top level to create the landscape view. Then created custom arrangements for the landscape view. In a method where my code detects a landscape orientation, my code points to the top of the landscape hierarchy and portrait when detecting portrait.
This works, except all the IBOutlets (UIView, UIButton, etc) in landscape hierarchy are still referencing their equivalent objects in portrait view hierarchy. I do a lot of special processing on these UI elements during the run time so I require IBOutlets and not just IBActions. I found that my copied UI elements in the landscape view can point to the same IBActions, but they can not share IBOutlets with the ones in the portrait hierarchy.
Do I have to duplicate all the objects and have my code manage everything (figure out which objects to use)?
Here’s one approach that may suit you.
If you load the landscape nib right at the time when you need to swap in the landscape view, you can pass your view controller as the nib file’s owner, and the nib loader will overwrite your view controller’s outlets based on the connections in the landscape nib.
And when you need to swap in the portrait view again, load the portrait nib and pass the view controller as the file’s owner. The nib loader will again overwrite your view controller’s outlets, this time based on the connections in the portrait nib.
You should lazily create a
UINibobject for each of the nibs the first time that nib is needed, and keep them around to speed up loading on subsequent rotations.