0

I'm using Google AdMob and to present ads, I do so from the ViewController. Right now, I have a button in my GameScene that is supposed to tell the ViewController to display the ads.

At first, I tried delegation, but that didn't work since SKScene already has a delegate, but then I stumbled upon this answer that showed a way for the SKScene to call a method in its ViewController. The answers seems sketchy, though, as the SKScene holds a reference to its ViewController. My understanding, as well as according to this, was that this is bad practice, assuming that in my case, the SKScene is the View in the MVC structure.

So how should I notify the ViewController of events occurring in the SKScene?

Minestrone-Soup
  • 399
  • 1
  • 16
  • Hint: notification. – El Tomato Mar 11 '18 at 00:42
  • @ElTomato I was going to use NSNotificationCenter but I thought it should only be used if I was broadcasting to more than one thing – Minestrone-Soup Mar 11 '18 at 00:43
  • You can use NotificationCenter however you like. There's no rulebook for it. In my app, I use delegates when there's only one listener, but for all the other cases, where there are multiple listeners, I just use NotificationCenter. Works great, and I haven't been struck by lightning yet. (Also, I think Apple advises that you use NotificationCenter instead of NSNotificationCenter, unless you need pass-by-reference semantics? Something like that. Maybe have a look before you start implementing your notifications.) – SaganRitual Mar 11 '18 at 05:42

1 Answers1

1

You can check my answer which describes how to communicate SKScene with UIViewController using delegate pattern.

Enrique Bermúdez
  • 1,740
  • 2
  • 11
  • 25