0
votes

I have the following files:

  • ViewController.xib
  • ViewController.h
  • ViewController.m
  • BannerView.xib
  • BannerView.h (inherits from UIView)
  • BannerView.m

My view controller is connected up and is working fine. I created BannerView and performed necessary linking as described in this tutorial: http://www.maytro.com/2014/04/27/building-reusable-views-with-interface-builder-and-auto-layout.html

However, in my ViewController.xib when I add a UIView and set it's class to the BannerView it just doesn't load. The app doesn't crash or anything.

Have I missed any steps, any help would be appreciated.

EDIT 1 - Added images and code

BannerView.h
  @property (nonatomic, strong) IBOutlet UIView *view;

#import "BannerView.h"

@implementation BannerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
   // Drawing code
}
*/

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        [self setup];
    }
    return self;
 }

- (void)setup {
    [[NSBundle mainBundle] loadNibNamed:@"BannerView" owner:self options:nil];
    [self addSubview:self.view];
}

@end

Banner View XIB - Files Owner banner view - files owner

Banner View - View banner view - view

1
Can you please show the code where "add a UIView and set it's class to the BannerView it just doesn't load."?tuledev

1 Answers

0
votes

Subclasses of UIView can't have their own XIB files. Notice when you go to create a subclass of UIView include XIB checkbox is not enabled. If you need to do custom drawing in a UIView overload drawRect. If you just want to add generic UIKit subviews to a view, do that in the XIB of the UIViewController the UIView will appear in or do it programmatically.