5

我已经实现了 AdMob,一切似乎都可以正常工作,
但我想知道,我怎样才能将横幅放在我所有的视图控制器中?
现在,我只在 RootViewController 上有横幅。
我总共有 4 个视图控制器。
谢谢。

4

2 回答 2

5

What you want here is a GADBannerView singleton of sorts. You can probably create a wrapping class to act as a singleton for your adView, so something like:

@interface GADMasterViewController : UIViewController {
  GADBannerView *adBanner_;
  BOOL didCloseWebsiteView_;
  BOOL isLoaded_;
  id currentDelegate_;
}

And just make sure that GADMasterViewController always returns a singleton:

+(GADMasterViewController *)singleton {
  static dispatch_once_t pred;
  static GADMasterViewController *shared;
  // Will only be run once, the first time this is called
  dispatch_once(&pred, ^{
    shared = [[GADMasterViewController alloc] init];
  });
  return shared;
}

Have a method which resets the current view controller that's holding on to the adView:

-(void)resetAdView:(UIViewController *)rootViewController {
  // Always keep track of currentDelegate for notification forwarding
  currentDelegate_ = rootViewController;

  // Ad already requested, simply add it into the view
  if (isLoaded_) {
    [rootViewController.view addSubview:adBanner_];
  } else {

    adBanner_.delegate = self;
    adBanner_.rootViewController = rootViewController;
    adBanner_.adUnitID = kSampleAdUnitID;

    GADRequest *request = [GADRequest request];
    [adBanner_ loadRequest:request];
    [rootViewController.view addSubview:adBanner_];
    isLoaded_ = YES;
  }
}

Then displaying your ad is just a matter of:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    shared = [GADMasterViewController singleton];
    [shared resetAdView:self];
}

You probably need to set up a delegate to forward notifications as well since the AdMob SDK doesn't act well to delegates changing on it in the middle of a request.

You can find a blog post about this here.

于 2012-04-10T20:59:07.867 回答
1

我不知道 adMob 是如何工作的,但是就像其他所有东西一样,你可以创建一个BaseViewController你可以添加你的 adMob(在viewDidLoad方法中)然后所有其他 viewControllers 可以子类 this 的东西BaseViewController。只需调用您的 viewControllers[super viewDidLoad];viewDidLoad方法,您就会拥有它...

希望这可以解决您的问题... :)

于 2012-04-10T16:20:48.540 回答