1

我有一个 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 cannot be nil”

这里发生了什么?提前致谢!

4

1 回答 1

0

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

于 2013-01-08T22:57:53.663 回答