我正在将 iAds 集成到一个带有 UITabBarController 和 UINavigationController 以及一堆基于 UITableView 的屏幕的 iOS 6 应用程序中,并且它主要处理一个明显的异常。当我从父视图控制器导航到子视图控制器时,即使我已经有一个广告,我的横幅视图也不会出现,直到调用 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 --> SubView --> 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];
}