简体   繁体   中英

How can I write and call a designated initializer for my view controller to initialize a private property, using storyboards?

I'd like to write a designated initializer in Swift for my view controller to initialize a private property. I understand how to do this in code, but I'm struggling to understand how to accomplish this with storyboards.

My custom game view controller is the root view controller of my nav controller, which is contained in a container view controller 在此处输入图片说明

Since all of the containment and view controller creation is happening automatically from the storyboard, the only solution I can think of is to declare my private property as a public property, so I can assign it a value after my game view controller has been created. This seems like a hack, since I'll only be declaring it as public in order to make use of storyboards. Am I understanding correctly, or is a point in the code when I can intervene and call my game view controller's designated initializer?

Thanks in advance for your wisdom!

Essentially, you can't. As you have found, when you instantiate a view controller from a storyboard, you don't have control over the initialiser that is used.

This is one of the barriers to using a true dependency injection pattern in Cocoa.

The best you can do is use prepareForSegue to set the properties on the newly created view controller.

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