iAd跨视图控制器(Singleton):不知何故不起作用

时间:2016-01-30 12:41:40

标签: ios objective-c iphone singleton iad

我很长时间以来一直在努力解决这个问题。我的错误在哪里?目前隐藏和表演似乎有效,但每次我来#34;回来"在我的viewcontroller中,我看到我的视图向上移动了,但那里没有广告。但是第一次看到视图控制器时,就有一则广告。我究竟做错了什么? 我只想在视图控制器上显示相同的广告,这就像父UIViewController类,许多其他视图控制器继承自:

#pragma mark View lifecycle
-(void)viewDidLoad
{
    [super viewDidLoad];

    if(![[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds]){
        self.bannerContainer = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).bannerView;
        self.bannerContainer.frame = CGRectOffset(self.bannerContainer.frame, 0, self.view.frame.size.height);
        [self.view addSubview:self.bannerContainer];
    }
}

//Handle the in app purchases
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    //iAd
    if(![[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds] && !self.bannerContainer){
        self.bannerContainer.delegate = self;
    }

    if(self.bannerContainer.bannerLoaded){
        [self showBanner];
    }

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkIAdPurchase) name:IAPHelperProductPurchasedNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self hideBanner];
    self.bannerContainer.delegate = nil;

    [[NSNotificationCenter defaultCenter] removeObserver:self name:IAPHelperProductPurchasedNotification object:nil];
}

#pragma mark Check iAd purchase
-(void)checkIAdPurchase
{
    if([[NSUserDefaults standardUserDefaults] objectForKey:kInAppPurchaseNoAds] && self.bannerContainer){
        [self hideBanner];
        [self.bannerContainer removeFromSuperview];
        self.bannerContainer.delegate = nil;
        self.bannerContainer = nil;
    }
}

#pragma mark IAd delegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    [self showBanner];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    [self hideBanner];
}


#pragma mark Show and hide the banner
- (void)showBanner
{
    if(!self.isBannerVisible){
        [self.view layoutIfNeeded];
        [UIView animateWithDuration:0.5
                         animations:^{
                             //Restore the constraint
                             self.mainViewBottomConstraint.constant = 50;
                             //Move the banner on
                             self.bannerContainer.frame = CGRectOffset(self.bannerContainer.frame, 0, -50);
                             [self.view layoutIfNeeded];
                         } completion:^(BOOL finished) {
                             self.isBannerVisible = YES;
                         }];
    }
}

- (void)hideBanner
{
    if(self.isBannerVisible){
        [self.view layoutIfNeeded];
        [UIView animateWithDuration:0.5
                         animations:^{
                             //Restore the constraint
                             self.mainViewBottomConstraint.constant = 0;
                             //Move the banner off
                             self.bannerContainer.frame = CGRectOffset(self.bannerContainer.frame, 0, self.bannerContainer.frame.size.height);
                             [self.view layoutIfNeeded];
                         } completion:^(BOOL finished) {
                             self.isBannerVisible = NO;
                         }];
    }
}

1 个答案:

答案 0 :(得分:0)

实际上你是在AppDelegate中创建iAd的实例,而且在整个app中只有一个。当你第一次来到Vc时,由于viewdidload中的代码,它会添加到你的Vc ..

现在当你移动到其他视图控制器时,viewcontroller也会在viewdidload中添加iAd。你只有一个iAd对象,所以它会在One VC中一次添加..所以当你移动到其他Vc iAd时会删除从那个Vc并添加到新的VC ..

解决方案:您应该在ViewWillAppear ...

中调用iAD子视图代码
相关问题