Search code examples
iosobjective-cios7xibsubview

Custom Subview on a iOS view has null outlets


I have a view called FeedView, handled by FeedViewController.

I also have a XIB called "NearestStore" which is handled by a view controller named "NearestStoreViewController". NearestStore xib has labels, buttons, etc. In the view controller I have outlets that are connected to the subviews in NearestStore.xib.

NearestStore inherits from UIButton (so it's easier to handle click event).

On FeedViewController.xib I have a UIButton that has been set to be of type NearestStore.

So far so good. This is on my FeedViewController:

__weak IBOutlet NearestStoreButton *btn_nearestStore;

The outlet is connected on the xib to the outlet.

NearestStoreViewController has several outlets to subviews like:

@property (nonatomic, weak) IBOutlet  UILabel *lbl_distance; 
@property (nonatomic, weak) IBOutlet  UIImageView *img_distance;

For some reason, on my FeedViewController the reference to btn_nearestStore is fine, but all the subviews are nil.

For example:

btn_nearestStore.lbl_distance 

is nil

What am I missing?


Solution

  • This sounds exactly as the system is supposed to work. It is not easy to create a custom widget using xibs.

    Here's how it works:

    Your FeedViewController will preform xib loading for the corresponding FeedView. During this load, it notices the NearestStoreButton subview. As a consequence, it creates such a view using the - (id)initWithCoder: message on the NearestStoreButton class. It will not magically notice the corresponding .xib nor the corresponding viewController.

    If you need to use a xib within a xib, you need to do the loading manually for all subviews. Keep in mind that you somehow need to create/use the appropriate owners (view controllers) for these secondary xibs.