如何制作没有xib或storyboard vc标识符的视图控制器? [英] How to make a view controller without a xib or a storyboard vc identifier?

查看:15
本文介绍了如何制作没有xib或storyboard vc标识符的视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在不使用 xibstoryboard 的情况下制作 viewcontroller.所以在这种情况下,我会得到 viewcontroller 这样的

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];

我知道它必须通过覆盖 init 方法来做一些事情,因为我们没有使用 initWithCoder ?

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

我知道我必须创建一个视图并将其设置为 PopupViewController 的 self.view,但我想知道如何才能做到这一点.

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.

是的,制作 xib 文件 或将 view controller 添加到 storyboard 可能要容易得多,这是为了深入了解视图控制器的工作原理.

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.

推荐答案

初始化视图的最佳位置是在 PopupViewControllerloadView 方法中.类似的东西:

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

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

然后,在 MyViewClass initWithFrame: 方法中构建所有子视图并为其设置约束.如果您不使用约束,请覆盖 layoutSubviews 方法.

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

这篇关于如何制作没有xib或storyboard vc标识符的视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆