How to make a view controller without a xib or a storyboard vc identifier?

喜你入骨 提交于 2019-12-12 04:19:03

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/34703569/how-to-make-a-view-controller-without-a-xib-or-a-storyboard-vc-identifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!