I use a singleton for an ad banner and call it into view on each ViewDidLoad. This automatically removes it from the previous view.
This is for adWhirl, but you should be able to adopt it for just iAD.
adWhirlSingleton.h
#import <Foundation/Foundation.h>
#import "AdWhirlDelegateProtocol.h"
#import "AdWhirlView.h"
@interface adWhirlSingleton : NSObject <AdWhirlDelegate> {
AdWhirlView *awView;
UIViewController *displayVC;
}
@property (strong, nonatomic) AdWhirlView *awView;
@property (strong, nonatomic) UIViewController *displayVC;
+(id)sharedAdSingleton;
-(void)adjustAdSize:(CGFloat)x:(CGFloat)y;
@end
adWhirlSingleton.m
#import "adWhirlSingleton.h"
@implementation adWhirlSingleton
static adWhirlSingleton* _sharedAdSingleton = nil;
@synthesize awView, displayVC;
+(id)sharedAdSingleton
{
@synchronized(self)
{
if(!_sharedAdSingleton)
_sharedAdSingleton = [[self alloc] init];
return _sharedAdSingleton;
}
return nil;
}
+(id)alloc
{
@synchronized([adWhirlSingleton class])
{
NSAssert(_sharedAdSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
_sharedAdSingleton = [super alloc];
return _sharedAdSingleton;
}
return nil;
}
-(id)init
{
self = [super init];
if (self != nil) {
// initialize stuff here
self.awView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
}
return self;
}
-(void)dealloc
{
displayVC = nil;
if (awView) {
[awView removeFromSuperview]; //Remove ad view from superview
[awView replaceBannerViewWith:nil];
[awView ignoreNewAdRequests]; // Tell adwhirl to stop requesting ads
[awView setDelegate:nil];
awView = nil;
}
}
-(void)adjustAdSize:(CGFloat)x :(CGFloat)y
{
[UIView beginAnimations:@"AdResize" context:nil];
[UIView setAnimationDuration:0.7];
awView.frame = CGRectMake(x, y, kAdWhirlViewWidth, kAdWhirlViewHeight);
[UIView commitAnimations];
NSLog(@"Recent Network Name: %@",[awView mostRecentNetworkName]);
}
-(BOOL)adWhirlTestMode
{
return YES;
}
-(NSString *)adWhirlApplicationKey
{
return @"xxxxxxxxxxxxx";
}
-(UIViewController *)viewControllerForPresentingModalView
{
return displayVC;
}
-(void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlView
{
NSLog(@"%s",__FUNCTION__);
NSLog(@"Recent Network Name: %@",[awView mostRecentNetworkName]);
//[self adjustAdSize];
}
-(void)adWhirlDidFailToReceiveAd:(AdWhirlView *)adWhirlView usingBackup:(BOOL)yesOrNo
{
NSLog(@"%s",__FUNCTION__);
}
@end
Then import adWhirlSingleton into each ViewController and in each viewWillAppear i just implement this:
adWhirlSingleton *adWhirlSingle = [adWhirlSingleton sharedAdSingleton];
adWhirlSingle.displayVC = self;
[adWhirlSingle adjustAdSize:0 :self.view.frame.size.height -50];
[self.view addSubview:adWhirlSingle.awView];
[self.view bringSubviewToFront:adWhirlSingle.awView];
NSLog(@"Ad Banner View");
but the view I have with a UITableView, I use this:
adWhirlSingleton *adWhirlSingle = [adWhirlSingleton sharedAdSingleton];
adWhirlSingle.displayVC = self;
[adWhirlSingle adjustAdSize:0 :self.tabBarController.view.frame.size.height -99];
[self.tabBarController.view addSubview:adWhirlSingle.awView];
NSLog(@"Should have added Ad!");
Hope that helps you a bit
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…