xjq233p_1
xjq233p_1

Reputation: 8070

How to load custom view in UIStoryBoard?

I am interested to write my custom view, so I created the following xib file:

enter image description here

This is the definition file:

- (void)_baseInit {
    NSLog(@"Unseen View loaded");
    [self addSubview:[self activityIndicator]];
    [self activityIndicator].alpha = 1.0;
    [self activityIndicator].frame = CGRectMake(round(([self imageView].frame.size.width - 25) / 2),
                                                round(([self imageView].frame.size.height - 25) / 2), 25, 25);
    [self activityIndicator].hidesWhenStopped = YES;
    [self showIndicator];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [self imageView].userInteractionEnabled = YES;
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [[self imageView] addGestureRecognizer:panRecognizer];

}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self _baseInit];
    }
    return self;
}

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

I tried to hook it up in my story board:

x

And I have my MainViewController call this during viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.unseenView = [[[NSBundle mainBundle] loadNibNamed:@"UnseenView" owner:self options:nil] objectAtIndex:0];
    self.unseenView.delegate = self;

Unfortunately, nothing is showing up in my simulator literally nothing, not even the text labels. enter image description here

However I am seeing the following log messages:

2013-02-20 17:37:58.929 Giordano.iPhone[66857:c07] Unseen View loaded
2013-02-20 17:37:58.934 Giordano.iPhone[66857:c07] Unseen View loaded

What am I doing wrong?

Upvotes: 1

Views: 546

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

It looks like you are preparing your view correctly, but you are not adding it to the view hierarchy. in the viewDidLoad code you need to add this line:

[self.view addSubview:unseenView];

Upvotes: 1

Related Questions