Where do you initialize property values โ€‹โ€‹in the view controller?

I have a view controller with properties that determine its behavior. They are set by the parent in prepareForSegue .

The potential error that occurs in my program is that the parent in question is not guaranteed to set properties, so I would like to have a default value. In research, I now understand that Objective-C properties do not have default values. This left me with the following options:

  • Set the default values โ€‹โ€‹for the properties in the child view method viewDidLoad . However, when investigating, viewDidLoad is called after prepareForSegue in the parent - so I would rewrite the parent values โ€‹โ€‹when the parent really sets the properties.
  • Then I thought that I could use (id) init to initialize the values, but at least when using the storyboard this method is not called at all!

I may have a workaround in that the objects will be initialized with the default value, but in this case all I want to pass is BOOL. And since bool will have some value, even if it was not correctly initialized, I cannot check it if it is not equal to zero.

Is it possible to initialize the value in the controller of the child view to prepareForSegue in the parent?

+6
source share
1 answer

Overriding init will not help, since it is not a designated initializer for UIViewController objects. When the manual creation of an instance of the view manager is called - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle , when loading from the storyboard - (id)initWithCoder:(NSCoder *)decoder . If you want to set properties when the view controller is initialized, you must override both initializers.

In research, I now understand that Objective-C properties do not have default values.

They make. nil for objects, 0 for integer values, 0.0 for floating points, etc. If you need default values โ€‹โ€‹other than this, you must set them in the appropriate init method.

+7
source

Source: https://habr.com/ru/post/957092/


All Articles