Question

I created a method in ViewController.h called - (void)showFullScreenAd.

I tried to call it inside my scene. I tried [self.view.window.rootController showFullScreenAd]. Can't find this method.

I tried ViewController *vc = [[ViewController alloc] init]

It did successfully NSLog "Interstitial Ad request". But no ad shown.

If I use the method directly in ViewController.m - viewDidLoad, it NSLog the same message "Interstitial Ad request" and display an interstitial Ad.

Was it helpful?

Solution

I would use notifications: the SKScene object posts a system-wide notification (NSNotification) to the notification center (NSNotificationCenter), and the targeted view controller just 'registers' (i.e., listens) to that same notification and implements a handler method. This is a pervasive pattern in Objective-C/Cococa.

Some benefits are:

  1. It is extensible: if in the future, you want to send the same message to the view controller form a different scene, just post the same notification from the new scene: no need to include headers and define a new property. Only need yo include the definition of the notification name.

  2. The view controller owns the view, and the view presents the scene. By not referencing the view controller from the scene, you avoid the risk of a reference cycle and hence a memory leak.

By the way, this is what I do in my games.

OTHER TIPS

A suggestion to how to use iAd;

in ViewController.m:

MyScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.viewController = self;

and then in your MyScene.h put those lines:

#import "ViewController.h"

@property (nonatomic) ViewController *viewController;

also don't forget to add iAd in ViewController.m;

[self.view addSubview:adView];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top