Ios addChildViewController:“在iPad上崩溃,但在iPhone上没有。”
我有一个iPhone应用程序,在手机上运行时效果很好,但在iPad上以兼容模式运行时,在启动时会崩溃。我在我的项目中使用iAd,特别是我正在使用的 Bannerviewcontroller.h和.m 从iAd套件以编程方式显示横幅 在我的AppDelegate中,我包装了一些视图控制器,如下所示Ios addChildViewController:“在iPad上崩溃,但在iPhone上没有。”,ios,ipad,crash,Ios,Ipad,Crash,我有一个iPhone应用程序,在手机上运行时效果很好,但在iPad上以兼容模式运行时,在启动时会崩溃。我在我的项目中使用iAd,特别是我正在使用的 Bannerviewcontroller.h和.m 从iAd套件以编程方式显示横幅 在我的AppDelegate中,我包装了一些视图控制器,如下所示 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UIViewController *vie
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
并引发此异常:
*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*-[\uu NSArrayM insertObject:atIndex:]:对象不能为零”
这是怎么回事?提前谢谢 错误表明,\u contentController未初始化。在哪里/如何分配?我在AppDelegate代码中看不到它。我编辑了我的原始帖子,以包含整个bannerviewcontroller.m。这是直接从苹果的iAd套件中获得的。谢谢。您使用UITabBarController作为参数调用BannerViewController的init方法-在示例中,他们使用选项卡栏控制器的各个视图调用它。请参见TabbedBanner/TabbedBanner/AppDelegate.mI中的示例实现。我决定采用不同的路线。由于这是一款iPhone应用程序,所以在兼容模式下运行时iAd将无法工作。一个简单的if语句检查设备是否为iPad。如果是,那么我将跳过bannerviewcontroller,只将选项卡分配给rootviewcontroller。如果是iPhone,那么我使用上面列出的bannerviewcontroller。好的不管怎样,你最初的问题已经得到了回答,对吗?
#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