2

我想在我的 viewController 中实现 iAd,以便我在这里为 iAd 创建单例类,这是我使用的代码,但它不会在我的 viewController 中显示 iAd。

adWhirlSingleton.h

#import <Foundation/Foundation.h>
#import "iAd/ADBannerView.h"
@interface adWhirlSingleton : NSObject <ADBannerViewDelegate> {
    ADBannerView *adView;
    UIViewController *displayVC;
}
@property (strong, nonatomic) ADBannerView *adView;
@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 adView, 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.adView.delegate=self;
}
return self;
}

-(void)dealloc
{
displayVC = nil;
if (adView) {
    [adView removeFromSuperview]; //Remove ad view from superview
    [adView setDelegate:nil];
    adView = nil;
}
[super dealloc];
}

-(void)adjustAdSize:(CGFloat)x :(CGFloat)y
{
   [UIView beginAnimations:@"AdResize" context:nil];
   [UIView setAnimationDuration:0.7];
   adView.frame = CGRectMake(x, y, 320, 50);
   [UIView commitAnimations];
}

-(BOOL)adWhirlTestMode
{
  return YES;
}

-(NSString *)adWhirlApplicationKey
{
  return @"xxxxxxxxxxxxx";
}

-(UIViewController *)viewControllerForPresentingModalView
{
  return displayVC;
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
  [self adjustAdSize:0 :410];
}

@end

我的视图控制器.m

#import "adWhirlSingleton.h"

-(void)viewWillAppear:(BOOL)animated {

   adWhirlSingleton *adWhirlSingle = [adWhirlSingleton sharedAdSingleton];
   adWhirlSingle.displayVC = self;
   [adWhirlSingle adjustAdSize:0 :self.view.frame.size.height -50];
   [self.view addSubview:adWhirlSingle.adView];
   [self.view bringSubviewToFront:adWhirlSingle.adView];
   NSLog(@"Ad Banner View");
}

这就是我实现单例类的方式,因为iAd当我执行此操作时,我没有iAd显示在我的 ViewController 上。如果有人知道如何实现这一点,请帮助我摆脱这个问题。提前致谢。

4

0 回答 0