简体   繁体   中英

Custom Control in iPhone Interface Builder

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.)

Where did you call the loadNibNamed:owner:options: method to load your Widget subviews from your Widget.XIB?

Also you did connect your views in your Widget.XIB file to the File's Owner (which is a Widget object) so the IBOutlets are connected and give you access to the views… but did you add the views as a subview to your Widgets?

Is the outlet nil when you try to access it using self.widget0.label ? Check if self.widget0 is not nil (I guess for that it is OK) but also if self.widget0.label is not nil too (I guess that's the real the problem). If it is, you probably forgot to load your Widget.xib for each widget view. If it isn't, then it probably means that you have your label, but it isn't added to the view hierarchy.

Your description of your XIB setup doesn't mention the label outlet on Widget being actually connected to its label in the XIB. Check that, maybe? It sounds as if you've got everything else set up, though you don't need to declare the IBOutlet attribute on the instance variable, just the property.

A common one I often miss is setting the class type to widget in IB.

在 IB 中选择的 Widget 类的屏幕截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM