Reputation: 165
I have a structure like this:
UIViewController
SCNView
SKScene
The ViewController
creates and holds a SCNView
and the SCNView
holds a SKScene
which is showing GUI
elements. You can click on buttons of the SKScene
to manipulate the UIViewController
. The SKScene
is calling functions of the UIViewController
.
This is where the cross reference is used, the SKScene
has a reference to the UIViewController
and the UIViewController
is referencing the SKScene
(through the SCNView
).
It is often suggested to avoid cross references, but how would I do it in this case?
I can think of passing callbacks or even polling for variable changes but I think that would make the code much less clean.
Upvotes: 2
Views: 99
Reputation: 52538
"It is often suggested to avoid cross references" - I have never heard that term used in iOS development, so that's news to me. You have to avoid reference cycles, which means you decide who is the owner of everything, and that owner holds strong references to whatever it wants to reference, but anything it references holds only a weak reference back.
Usually you wouldn't hold a reference to a view controller. Instead you define a protocol with exactly the functionality that your SKScene needs to get supplied from someone else, your view controller supports the protocol, and the SKScene gets a delegate supporting that protocol. Much more flexible.
Upvotes: 2