Search code examples
iosobjective-cuiviewxcode6xcode-storyboard

UIView not displaying on storyboard


I've created a UIView(KGCalloutView) and placed it in my Storyboard(Main.storyboard)by dragging on a View object and setting the class to KGCalloutView. When I run the project however the view is never displayed in the storyboard.

My first attempt to resolve entailed adding the following method, however it resulted in I believe an infinite loop(understandably) and eventually crashed.

- (void)awakeFromNib {
    if ([[NSBundle mainBundle] loadNibNamed:@"KGCalloutView" owner:self options:nil]) {
        [self.view setFrame:[self bounds]];
        [self addSubview:self.view];
    }
}

Next I tried adding the init method below, but did not resolve the issue either:

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

    return self;
}

I tried to post images of my .xib and Main.storyboard but I guess my reputation isn't high enough yet.

Here is KGCalloutView.h:

@interface KGCalloutView : UIView <UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic) IBInspectable NSString *title;
@property (strong, nonatomic) IBOutlet KGCalloutView *view;

@property (weak, nonatomic) IBOutlet UITextField *titleField;
@property (weak, nonatomic) IBOutlet UIPickerView *typePickerView;

@end

Here is KGCalloutView.m:

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

- (void)awakeFromNib {
    if ([[NSBundle mainBundle] loadNibNamed:@"KGCalloutView" owner:self options:nil]) {
        [self.view setFrame:[self bounds]];
        [self addSubview:self.view];
    }
}

-(void)layoutSubviews{
    self.titleField.text = @"test";
    self.typePickerView.dataSource = self;
    self.typePickerView.delegate = self;
}

Here's my ViewController.h:

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet KGCalloutView *callout;

@end

Here's my ViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];
}

Solution

  • I wasn't able to get a working answer so I just did my nib loading in the ViewController instead of my custom view :/