我有多个viewControllers,我想运行iAd。 iAd已正确设置并正在使用第一个控制器,但我收到了委派错误。
adView.delegate = self;
[adView setHidden:YES];
答案 0 :(得分:0)
为您的广告创建单身人士。这是你的.h:
#import <iAd/iAd.h>
@interface AEMIAd : NSObject <ADBannerViewDelegate>{
ADBannerView *adView;
BOOL bannerIsVisible;
}
@property (nonatomic, strong) ADBannerView *adView;
+(AEMIAd *)sharedAd;
@end
和你的.m:
#import "AEMIAd.h"
static AEMIAd *sharedAd = nil;
@implementation AEMIAd
@synthesize adView;
- (id)init
{
if (self)
{
// On iOS 6 ADBannerView introduces a new initializer, use it when available.
if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)])
{
adView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else adView = [[ADBannerView alloc] init];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
adView.frame = CGRectOffset(adView.frame, 0.0f, screenBounds.size.height-50.0f);
adView.delegate = self;
}
return self;
}
-(BOOL)allowActionToRun
{
return TRUE;
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self.adView setHidden:NO];
bannerIsVisible = YES;
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[self.adView setHidden:YES];
bannerIsVisible = NO;
}
+(AEMIAd *)sharedAd
{
if (!sharedAd) sharedAd = [[self alloc] init];
return sharedAd;
}
每个viewControllers中的延迟实例化:
-(ADBannerView *)adView
{
if (!_adView) _adView = [[AEMIAd sharedAd] adView];
return _adView;
}
和
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[adView removeFromSuperview];
}
就是这样。