简体   繁体   English

自定义UIViewController init与init相关的数据

[英]Custom UIViewController init with init related data

I have a question regarding the custom init methods specifically on UIViewControllers . 我有一个关于UIViewControllers上的自定义init方法的问题。 Let's say that I write an init method which takes one parameter (let's say UIViewControllerInitMode ) and that parameter is responsible to indicate in which way the view should load. 假设我编写了一个带有一个参数的init方法(比如说UIViewControllerInitMode ),该参数负责指示视图应以哪种方式加载。 So when the viewDidLoad gets called that parameter (now stored as a class variable) gets checked and the GUI related content is loaded accordingly. 因此,当调用viewDidLoad时,将检查该参数(现在存储为类变量),并相应地加载与GUI相关的内容。 How is this done? 怎么做?

Let's take this example: 让我们来看这个例子:

We have a NS_ENUM called UIViewControllerInitMode with modes kUIViewControllerInitModeOne and kUIViewControllerInitModeTwo . 我们有一个NS_ENUM称为UIViewControllerInitMode与模式kUIViewControllerInitModeOnekUIViewControllerInitModeTwo Now for the init and viewDidLoad code: 现在查看initviewDidLoad代码:

- (instancetype)initWithMode:(UIViewControllerInitMode)m
{
    self = [super init];
    if (self)
    {
         mode = m; //Assume that mode is a class variable
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (mode == kUIViewControllerInitModeOne) //Check the class variable
        self.view.backgroundColor = [UIColor blueColor];
    else //It's the InitModeTwo
        self.view.backgroundColor = [UIColor redColor];
}

Now this looks like a perfectly legitimate piece of code (at least to me) but if my knowledge is correct the actual view of a UIViewController gets lazy loaded so there is no telling if the view is blue or red when the class variable gets set (except with an extra if but that looks ugly because it would mean I have the same code for GUI in init and in viewDidLoad ). 现在,这看起来像是一段完全合法的代码(至少对我来说是这样),但是如果我的知识是正确的,则将UIViewController的实际视图延迟加载,因此在设置类变量时,无法判断视图是蓝色还是红色(除非有一个额外的if,但这看起来很难看,因为这意味着我在initviewDidLoad具有相同的GUI代码。 So does this mean that under some circumstances the view can have a red background even if I init ed the controller with the mode that should make a blue background? 那么,这是否意味着在某些情况下,即使我使用应该使背景变成蓝色的模式init了控制器,该视图也可能具有红色背景? Setting the background color in init is not safe for the same reason or is it? 出于同样的原因,在init设置背景颜色还是不安全的? It always works if I do it in the way of the example above but I want to get to the bottom of this. 如果我按照上述示例的方式进行操作,它总是可以工作的,但是我想深入了解这一点。 How does this happen under the hood? 这是怎么发生的呢? Where am I right and/or wrong? 我在哪里是对还是错?

You example is perfectly fine. 您的例子很好。 You can set the views properties after it is created. 您可以在创建视图属性后对其进行设置。 When you changes don't show up, you can ask the view to draw itself again by calling 当您的更改未显示时,您可以通过调用

[self.view setNeedsDisplay];

Setting the background color in init would fail, I guess, because the view isn't initialized in init yet. 我猜在init中设置背景颜色会失败,因为视图尚未在init中初始化。 But I'm not sure about this. 但是我不确定。 Just try it. 去尝试一下。

I see no problem with the code above. 我认为上面的代码没有问题。 You are right that you should not touch UI components in the init as they are most likely null at that time. 没错,您不应该触摸init UI组件,因为那时UI组件很可能为null The initWithMode method should not care what color the view is as it is probably no color at all. initWithMode方法不应该在乎视图是什么颜色,因为它可能根本没有颜色。 The viewDidLoad method is for changing the display properties of the view and other UI components. viewDidLoad方法用于更改视图和其他UI组件的显示属性。 Due to the fact that the init will always be called before the viewDidLoad , your view should always be set to either red or blue based on mode . 由于init将始终在viewDidLoad之前被调用,因此您应根据mode将视图始终设置为红色或蓝色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM