This appears to be a common question, but I haven't seen it answered clearly or succinctly - and as such I'm having trouble making it work.
I have created a Widget.xib file in Interface Builder. It consists of:
View (UIView)
ImageView (UIImageView)
Button (UIButton)
Label (UILabel)
I have created Widget.h and Widget.m, which implement class Widget, which derives from UIView. In Interface Builder, Widget.xib has its File's Owner set to the Widget class.
Widget.h looks like this:
@interface IconView : UIView {
IBOutlet UIButton *widgetButton;
IBOutlet UILabel *widgetLabel;
IBOutlet UIView *widgetView;
IBOutlet UIImageView *widgetImage;
}
@property (nonatomic, retain) IBOutlet UIButton *widgetButton;
@property (nonatomic, retain) IBOutlet UILabel *widgetLabel;
@property (nonatomic, retain) IBOutlet UIView *widgetView;
@property (nonatomic, retain) IBOutlet UIImageView *widgetImage;
Widget.h includes:
@synthesize widgetButton, widgetLabel, widgetView, widgetImage;
And its dealloc releases those four objects. In Interface Builder, the button, label, view, and imageview are all connected to the appropriate IBOutlets.
Back in Interface Builder, I've opened another XIB from the project. This is Playfield.xib. It's a more traditional view, backed with PlayfieldViewController.h and .m. It contains:
View (UIView)
ImageView (with a static graphic) (UIImageView)
Button (UIButton)
Widget
Widget
Widget
In Playfield.h, I've created IBOutlets for the button and the three Widgets.
@interface LinkViewController : UIViewController {
IBOutlet UIButton *buttonBack;
IBOutlet Widget *widget0;
IBOutlet Widget *widget1;
IBOutlet Widget *widget2;
}
@property (nonatomic, retain) IBOutlet Widget *widget0;
@property (nonatomic, retain) IBOutlet Widget *widget1;
@property (nonatomic, retain) IBOutlet Widget *widget2;
- (IBAction)buttonBackClicked:(id)sender;
These are also synthesized and released in Playfield.h. In Interface Builder, Playfield.xib shows three empty rectangles where the widgets have been placed, which is what I expected. Those are wired up to their corresponding IBOutlets in Playfield.h.
So, at this point I assume everything is ready to go. In Playfield.m's viewDidLoad, I do this:
self.widget0.label.text = @"Feed Me";
In my mind, that references the widget0 IBOutlet declared in Playfield.h. That connects to the first Widget item in Playfield.xib. That, in turn, has a declared label IBOutlet in Widget.xib and Widget.h. A UILabel has a Text property. So it should change the text on the label within the widget.
But it doesn't. What am I doing wrong?
(I want to emphasize that I'm not looking to make an Interface Builder plugin. I know those are complicated. Within IB, I'm fine manipulating my Widgets as empty rectangles. I'll also note that I've set the background color for the widget's View, in Widget.xib, to white. When I run the project, I can clearly see the white rectangles atop the playfield graphic, so I know the widgets are "in" there. I just can't seem to access them.)
