0

I wanted to know how to make a viewcontroller without the use of xib or storyboard. So in this case, I would be getting the viewcontroller as such

   PopupViewController* vc = [[PopupViewController alloc] init];
    [self addChildViewController:vc];
    [self.view addSubview:vc.view];
    [vc didMoveToParentViewController:self];

I know it has to do something with overriding the init method since we are not using initWithCoder ?

I know I have to create a view and set it as the self.view for the PopupViewController, but I was wondering how I could do that.

EDIT: Yes it may be much easier just to make an xib file or add a view controller to the storyboard, this is to understand deeply how view controllers work.

Hiren Dhamecha
  • 658
  • 5
  • 15
Lucky
  • 579
  • 6
  • 24
  • add frame to your viewcontroller while allocating or before adding to superview. Also do some view customization like background color etc for your view. – Gagan_iOS Jan 10 '16 at 09:27
  • Possible duplicate of [Is there a way to create the controller and the UIView programatically and not use the interface builder?](http://stackoverflow.com/questions/5778856/is-there-a-way-to-create-the-controller-and-the-uiview-programatically-and-not-u) – Cristik Jan 10 '16 at 10:16

1 Answers1

2

The best place to init the view is in PopupViewController's loadView method. Something like:

- (void)loadView {
   self.view = [MyViewClass new];
}

Then, in MyViewClass initWithFrame: method build all subviews and set constraints to it. If you're not using constraints, override layoutSubviews method.

vdugnist
  • 371
  • 2
  • 7