Eric
Eric

Reputation: 4061

Draw SubView in Storyboard

I want to present a modalView (it can be a viewController) that I draw in Storyboard. I don't want to have to make the whole thing programmatically. Is there a way to do this without it being a full screen view?

I guess another way to ask the question is: how do I [self.view addSubView:mySubView] where mySubView is drawn in InterFaceBuilder/Storyboard?

Upvotes: 1

Views: 423

Answers (2)

Md Mahbubur Rahman
Md Mahbubur Rahman

Reputation: 2075

Override the initWithCoder method in the object-c class.

- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
    [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0]];
}
return self;

}

Upvotes: 0

ikuramedia
ikuramedia

Reputation: 6058

To do this properly, you should look at View Controller containment in the docs. Basically you would addChildViewController after instantiating the viewController from your storyboard and then add the viewController's view to your current view hierarchy.

To just get it working however, the following will get you going:

UIViewController *childViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"<identifier you set in Interface Builder>"];
[self.view addSubView:childViewController.view];

Note that one of the reasons to do it 'properly' will be to ensure that autorotation and presentation callbacks are sent to the sub view controller.

Upvotes: 1

Related Questions