如何使用singleton类实现iAd

时间:2012-05-10 07:12:07

标签: objective-c ios5 singleton iad xcode4.3

我想在我的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

  

myViewController.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的单例类的方法,当我执行此操作时,我没有在我的ViewController上显示iAd。 如果有人知道如何实现这一点,请帮助我摆脱这个问题。在此先感谢。

0 个答案:

没有答案