Programmatic reference to a nib instantiated UI object


#1

In Chapter 9, the first comment in the example code for the “For the More Curious: Overriding Autorotation” exercise has really thrown me: “Assume ‘button’ is a subview of this view controller’s view”. I tried to apply this exercise to the HeavyViewController project earlier in the chapter, but I can’t figure out how to get a reference to one of the UIButtons in the nib.

I guess my question is this: how would you get a reference to a UIButton archived in a nib so that you could manipulate it programatically in a view controller subclass’s willAnimateRotationToInterfaceOrientation:duration:? I naively thought I could just add a UIButton pointer as a UIOutlet in my view controller subclass and bind that outlet to the UIButton in the nib. My understanding was that when the nib loaded, the UIButton would be instantiated and then when the HeavyViewController instance is inserted as the File’s Owner, my outlet binding would wire up the UIButton by assigning the instantiated button to my “button” pointer. Is my understanding flawed?


#2

You should be able to do what you describe, by assigning an IBOutlet in your view controller to a button in the XIB that is owned by that view controller. I just verified this by implementing said method to change the backgroundColor of a UIButton.

Did you hook up the IBOutlet to the correct button? File’s Owner is definitely your view controller? And last but not least, did you save your changes in IB? I occasionally forget to save after making a small tweak to a XIB, and then see unpredictable behavior.

In my XIB, File’s Owner is HeavyViewController. It has two outlets, view and button.