最初不为子视图显示的共享iAd实例

时间:2013-07-21 19:54:18

标签: uitableview ios6 uinavigationcontroller uitabbarcontroller iad

我正在将iAds集成到iOS 6应用程序中,其中包含UITabBarController和UINavigationController以及一系列基于UITableView的屏幕,并且它主要使用一个明显的异常。当我从父视图导航到子视图控制器时,直到调用ADBannerView委托方法(提供新广告时),即使我已经有广告,我的横幅视图也不会显示。

我想要的是当我在家长上显示广告并点击该广告时,我希望该广告立即显示。但是,正在发生的事情是广告横幅消失并重新出现,直到调用委托方法来更新它。单步执行调试器,它看起来应该可以工作。我确信这是一件微不足道的事我不做,但我被卡住了。救命啊!

正如我在评论中所说,似乎是_contentView框架大小在代码将广告放置在视图中时是不正确的。关闭自动布局后,它会最初将广告设置为正确,但对于每个新广告,横幅会向上移动到屏幕上。启用自动布局后,广告的初始显示位置错误,但广告的后续调用(或轮换调用)位于正确的位置。

以下是相关代码:

我有一个单例类来管理我想要共享的ADBannerView实例的创建。单身只做一次分配;否则它返回横幅视图:

- (ADBannerView *) getAdBannerSingleton
{
    if ( bannerView == nil )
    {
        DDLogInfo(@"iAds: Creating our initial banner view") ;
        bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner] ;

        // to handle rotation properly
        [bannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth] ;
    }

    return bannerView ;
}

我有一个具有View的基类 - >子视图 - > UITableView(在XIB中)并管理ADBannerView并充当委托。我的具体父/子类继承自基类,但不做任何特殊的事情(他们幸福地没有意识到广告)。

@implementation SSSAdEnabledTableViewController

@synthesize tableView = _tableView;
@synthesize contentView = _contentView;
@synthesize bannerView = _bannerView ;

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil] ;

    // this class does not need to do anything special as there are no concrete instances of it

    return self ;
}

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

    //
    // manage our ad
    //
    DDLogInfo(@"iAds: Managing ad View from viewWillAppear for %@", 
                                            NSStringFromClass([self class])) ;
    [self manageAdView] ;
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated] ;
    // no effect with our without the call below
    [_bannerView removeFromSuperview];
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    [_tableView setAutoresizesSubviews:YES] ;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

    self.contentView = nil ;
    self.tableView = nil ;
    self.bannerView = nil ;
}

#pragma mark -- iAd framework methods

- (void) bannerViewDidLoadAd:(ADBannerView *)banner
{
    DDLogInfo(@"iAds: Banner ad successfully loaded for %@", NSStringFromClass([self class])) ;
    [self manageAdView] ;
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    return YES ;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
    // don't think we have to do anything?
}

- (void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    DDLogError(@"Error loading iAd for %@. Reason: %@",
               NSStringFromClass([self class]),
               [error localizedDescription]) ;

    [self manageAdView] ;
}

#pragma mark -- ad banner helper methods

//
// Get our shared banner view instance
//
- (void)createAdBannerView
{
    // get the banner view instance
    _bannerView = [[SSSStoreManager sharedStore] getAdBannerSingleton] ;

    // make us the delegate
    DDLogInfo(@"iAds: Setting %@ to be our banner delegate", NSStringFromClass([self class])) ;

    // set it to be the delegate
    [_bannerView setDelegate:self] ;
}

//
// Based on the ad status, make room for it in our view or hide it
//
- (void)manageAdView
{
    // make sure we always have a banner view to work with here
    if ( _bannerView == nil )
    {
        DDLogInfo(@"Banner View is Nil; Creating it for %@", NSStringFromClass([self class]));
        [self createAdBannerView] ;
    }

    // will hold our banner dimensions and position
    CGRect bannerFrame = CGRectZero;
    // our inner view dimensions and position
    CGRect contentViewFrame = _contentView.frame;

    // make sure all of the changes happen at one time
    [UIView beginAnimations:@"fixupViews" context:nil];

    // is the banner loaded and should it be visible?
    if ( _bannerView.bannerLoaded == YES )
    {
        DDLogInfo(@"iAds: Showing banner view for %@", NSStringFromClass([self class])) ;
        bannerFrame.size = [_bannerView sizeThatFits:contentViewFrame.size];

        contentViewFrame.size.height -= bannerFrame.size.height;
        bannerFrame.origin.y = contentViewFrame.size.height;
    }
    else
    {
        DDLogInfo(@"iAds: Hiding banner view for %@", NSStringFromClass([self class])) ;
        bannerFrame.origin.y = contentViewFrame.size.height;
    }

    // reset our content view based on whether we have an ad to show
    _contentView.frame = contentViewFrame ;

    [self.view addSubview:_bannerView];
    _bannerView.frame = bannerFrame;

    [UIView commitAnimations];
}

0 个答案:

没有答案