How to avoid making every viewController an adWhirl delegate? (is there an AdWhirl design pattern)

328 Views Asked by At

Background: I asked a question last week about using adWhirl across multiple viewControllers but unfortunately I'm still not getting it.

I took the suggestions and now use one adWhirl instance, created in the app delegate, in each viewController but I don't see how I can respond to the adWhirl delegate events in my viewController (adWhirlDidReceiveAd:, adWhirlDidFailToReceiveAd: etc.) since the app delegate doesn't know which viewController is currently displaying its adView.

Hence my confusion...

Is there a design pattern for this? I would think there must be. I don't want to lard all my viewControllers with boilerplate adWhirl delegate code if I can avoid it.

What I've got now in each viewController is below. I tried setting up a method in the app delegate that each view controller could call to "register" that is was taking ownership of the app delegate's adView - thinking that when the delegate receives events like adWhirlDidReceiveAd, it could do the work below. This sort of worked but lead me to think I was reinventing the wheel.

Question: Is there a design pattern for using the adWhirl view across multiple viewControllers?

- (void)viewDidLoad
{
[super viewDidLoad];

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

[self.view addSubview:appDelegate.adView];
[self.view bringSubviewToFront:appDelegate.adView];

CGSize adSize = [appDelegate.adView actualAdSize];
CGRect onScreenFrame = appDelegate.adView.frame;
onScreenFrame.size.height = adSize.height; // fit the ad 
onScreenFrame.size.width = adSize.width; 
onScreenFrame.origin.x = (self.view.frame.size.width - adSize.width)/2; // center 
onScreenFrame.origin.y = (self.view.frame.size.height - adSize.height); 

appDelegate.adView.frame = onScreenFrame;

}


- (void)viewDidDisappear:(BOOL)animated
{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.adView removeFromSuperview];
}
1

There are 1 best solutions below

0
On BEST ANSWER

The obvious answer is to make a Controller class that all your view controllers will become subclasses of. So then

AdviewDelegateController : UIViewController

and

YourViewController : AdviewDelegateController

then you put all your adview logic in the AdviewDelegateController.