“addChildViewController:”在iPad上崩溃但不是iPhone崩溃

时间:2013-01-08 18:00:12

标签: ios ipad crash

我有一个iPhone应用程序在手机上运行时效果很好,但在iPad上以兼容模式运行时会在启动时崩溃。我在我的项目中使用iAd,特别是我正在使用

  

Bannerviewcontroller.h和.m

从iAd套件以编程方式显示横幅。

在我的App Delegate中,我按如下方式包装一些视图控制器。

 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不能为nil'

这是怎么回事?提前谢谢!

1 个答案:

答案 0 :(得分:0)

错误告诉我,_contentController未初始化。你在哪里/如何分配它?我无法在AppDelegate代码中看到它。