简体   繁体   中英

Custom UIViewController init with init related data

I have a question regarding the custom init methods specifically on UIViewControllers . 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. So when the viewDidLoad gets called that parameter (now stored as a class variable) gets checked and the GUI related content is loaded accordingly. How is this done?

Let's take this example:

We have a NS_ENUM called UIViewControllerInitMode with modes kUIViewControllerInitModeOne and kUIViewControllerInitModeTwo . Now for the init and viewDidLoad code:

- (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 ). 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? Setting the background color in init is not safe for the same reason or is it? 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. 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. The initWithMode method should not care what color the view is as it is probably no color at all. The viewDidLoad method is for changing the display properties of the view and other UI components. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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