2013-01-08 78 views
1

我有一款iPhone應用程序在手機上運行時效果很好,但在iPad上以兼容模式運行時在啓動時崩潰。我用的iAd在我的項目,特別是我使用「addChildViewController:」在iPad上崩潰但不是iPhone

Bannerviewcontroller.h從iAd的套件.M

以編程方式呈現的橫幅。

在我的應用程序委託我包裝一些視圖控制器如下。

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
UIViewController *viewController1, *viewController2, *catVC3, *otherVC4; 
UINavigationController *navController, *dognav, *catnav, *othernav; 
    viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil]; 
    viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil]; 
    catVC3 = [[catViewController alloc]initWithNibName:@"catViewController_iPhone" bundle:nil]; 
    otherVC4 = [[otherViewController alloc]initWithNibName:@"otherViewController_iPhone" bundle:nil]; 

    navController = [[UINavigationController alloc]initWithRootViewController:viewController1]; 
    dognav = [[UINavigationController alloc]initWithRootViewController:viewController2]; 
    catnav = [[UINavigationController alloc]initWithRootViewController:catVC3]; 
    othernav = [[UINavigationController alloc]initWithRootViewController:otherVC4]; 


_tabBarController = [[UITabBarController alloc] init]; 
NSArray *controllers = [NSArray arrayWithObjects:navController,dognav,catnav,othernav,nil]; 
_tabBarController.viewControllers = controllers; 

//wrap tabbar controller in adbanner controller 
_bannerViewController = [[BannerViewController alloc] initWithContentViewController:_tabBarController]; 
self.window.rootViewController = _bannerViewController; 
[self.window makeKeyAndVisible]; 
return YES; 

然後崩潰發生在我這裏

Bannerviewcontroller.m

#import "BannerViewController.h" 


@implementation BannerViewController 
{ 
    ADBannerView *_bannerView; 
    UIViewController *_contentController; 
} 

- (id)initWithContentViewController:(UIViewController *)contentController 
{ 
    self = [super init]; 
    if (self != nil) { 
     _bannerView = [[ADBannerView alloc] init]; 
     _bannerView.delegate = self; 
     _contentController = contentController; 
    } 
    return self; 
} 

- (void)loadView 
{ 
    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    [contentView addSubview:_bannerView]; 
    [self addChildViewController:_contentController]; 
    [contentView addSubview:_contentController.view]; 
    [_contentController didMoveToParentViewController:self]; 
    self.view = contentView; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return [_contentController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; 
} 

- (void)viewDidLayoutSubviews 
{ 
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) { 
     _bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; 
    } else { 
     _bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape; 
    } 
    CGRect contentFrame = self.view.bounds; 
    CGRect bannerFrame = _bannerView.frame; 
    if (_bannerView.bannerLoaded) { 
     contentFrame.size.height -= _bannerView.frame.size.height; 
     contentFrame.origin.y = _bannerView.frame.size.height; 
     bannerFrame.origin.y = contentFrame.size.height - contentFrame.size.height; 
    } else { 
     bannerFrame.origin.y = contentFrame.size.height; 
    } 
    _contentController.view.frame = contentFrame; 
    _bannerView.frame = bannerFrame; 
} 

- (void)bannerViewDidLoadAd:(ADBannerView *)banner 
{ 
    [UIView animateWithDuration:0.35 animations:^{ 
     [self.view setNeedsLayout]; 
     [self.view layoutIfNeeded]; 
    }]; 
} 

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error 
{ 
    [UIView animateWithDuration:0.35 animations:^{ 
     [self.view setNeedsLayout]; 
     [self.view layoutIfNeeded]; 
    }]; 
} 


@end 

,並拋出這個異常: *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因:'* - [__ NSArrayM insertObject:atIndex:]:object can not be nil'

這是怎麼回事?提前致謝!

回答

0

錯誤告訴,_contentController未初始化。你在哪裏/如何分配?我無法在AppDelegate代碼中看到它。

+0

我編輯了我的原始文章,包括整個bannerviewcontroller.m。這直接來自Apple的iAd套件。謝謝。 – kev

+0

您可以使用'UITabBarController'作爲參數調用'BannerViewController'的'init'方法 - 在這些例子中,它們用標籤欄控制器的單獨視圖調用它。請參閱TabbedBanner/TabbedBanner/AppDelegate.m中的示例實現 – SAE

+0

我決定採取不同的路線。由於這是一款iPhone應用程序,無論如何,iAd在兼容模式下運行時都不會工作。一個簡單的if語句會檢查設備是否是iPad。如果是的話,我可以一起跳過bannerviewcontroller,並將標籤分配給rootviewcontroller。如果它是iPhone,那麼我使用上面列出的bannerviewcontroller。 – kev

相關問題