如何避免使每个视图控制器都成为adWhirl委托?(是否有AdWhirl设计模式)



背景:上周我问了一个关于在多个视图控制器中使用adWhirl的问题,但不幸的是我仍然没有得到它。

我采纳了建议,现在在每个视图控制器中使用一个在应用程序委托中创建的 adWhirl 实例,但我看不到如何响应我的视图控制器(adWhirlDidReceiveAd:, adWhirlDidFailToReceiveAd:等)中的 adWhirl 委托事件,因为应用程序委托不知道哪个视图控制器当前正在显示其 adView。

因此我的困惑...

有没有设计模式?我认为一定有。如果可以避免的话,我不想用样板 adWhirl 委托代码来猪油我所有的视图控制器。

我现在在每个视图中得到的内容控制器如下。我尝试在应用程序委托中设置一种方法,每个视图控制器都可以调用该方法来"注册",即获得应用程序委托的 adView 的所有权 - 认为当委托收到类似 adWhirlDidReceiveAd 的事件时,它可以完成以下工作。这种方式奏效了,但让我觉得我正在重新发明轮子。

问:是否存在跨多个视图控制器使用 adWhirl 视图的设计模式?

- (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];
}

显而易见的答案是创建一个控制器类,所有视图控制器都将成为其子类。那么那么

AdviewDelegateController : UIViewController

YourViewController : AdviewDelegateController

然后,将所有 adview 逻辑放入 AdviewDelegateController 中。

最新更新