Of course, this will work, but the only problem is that when updating a delegate for an announcement, he will not actually take a new delegate for the announcement unless you explicitly make a new request for the announcement, so your old views will receive any notifications from the announcement. I would recommend using a single-mode adMob, which then forwards any delegate notifications to the correct view.
So, create a class called GADMasterViewController
(make sure it also follows the GADBannerViewDelegate
protocol) or something that has a static initializer:
+(GADMasterViewController *)singleton { static dispatch_once_t pred; static GADMasterViewController *shared = nil; dispatch_once(&pred, ^{ shared = [[GADMasterViewController alloc] init]; }); return shared; }
Then in the initializer you can initialize one GADBannerView as a property of this singleton:
-(id)init { if (self = [super init]) { self.adBanner = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, 0.0, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
Then you can have a method that sets the new adView as currentDelegate as such:
-(void)resetAdView:(UIViewController<GADBannerViewDelegate> *)rootViewController { if (self.isLoaded) { currentDelegate_ = rootViewController; [rootViewController.view addSubview:self.adBanner]; } else { // The delegate to forward any notifications too currentDelegate_ = rootViewController; self.adBanner.delegate = self; self.adBanner.rootViewController = rootViewController; self.adBanner.adUnitID = kSampleAdUnitID; GADRequest *request = [GADRequest request]; [self.adBanner loadRequest:request]; [rootViewController.view addSubview:self.adBanner]; self.isLoaded = YES; } }
At this point, you just want to redirect any notifications you receive to the correct viewController, so one example:
- (void)adViewDidReceiveAd:(GADBannerView *)view { if ([currentDelegate_ respondsToSelector:@selector(adViewDidReceiveAd:)]) { [currentDelegate_ adViewDidReceiveAd:view]; } }
In ViewControllerX
(one of your 4 ViewControllers), you can simply add it to your view hierarchy using:
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; shared = [GADMasterViewController singleton]; [shared resetAdView:self]; }