我已经实施了AdMob&一切似乎都有效,
但我想知道,我怎么能把横幅放在我的所有视角控制器中?
现在,我只在RootViewController上有横幅.
我总共有4个视图控制器.
谢谢.
但我想知道,我怎么能把横幅放在我的所有视角控制器中?
现在,我只在RootViewController上有横幅.
我总共有4个视图控制器.
谢谢.
解决方法
你想要的是一个GADBannerView单例.您可以创建一个包装类来充当adView的单例,如下所示:
@interface GADMasterViewController : UIViewController { GADBannerView *adBanner_; BOOL didCloseWebsiteView_; BOOL isLoaded_; id currentDelegate_; }
并确保GADMasterViewController始终返回一个单例:
+(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; }
-(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; } }
然后展示您的广告只需要:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; shared = [GADMasterViewController singleton]; [shared resetAdView:self]; }