[英]iOS app crashing when clicking on a tab in tabbarcontroller
我正在为iPhone创建iOS应用,但是我正在使用的UITabBarController遇到问题。 每当我单击选项卡栏中的选项卡时,程序就会崩溃。 我没有使用情节提要或类似的东西,而是选择通过代码来完成所有事情。 就是说,这里是设置标签的代码。
- (void) loadView
{
// create main view
UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor grayColor];
self.view = contentView;
// now create the tab pages
StatusViewController *status = [[StatusViewController alloc] initWithNibName: nil bundle: nil];
OperationsViewController *operations = [[OperationsViewController alloc] initWithNibName: nil bundle: nil];
self.statusTab = status;
self.operationsTab = operations;
status.title = @"Status";
operations.title = @"Operations";
// create the tab bar controller and add all tabs
UITabBarController *tabbar = [[UITabBarController alloc] init];
tabbar.view.frame = CGRectMake(0, 0, 320, 460);
NSMutableArray *tabs = @[status, operations];
[tabbar setViewControllers:tabs];
[self.view addSubview: tabbar.view];
}
选项卡确实在应用程序底部正确显示,默认情况下,通过单击任何选项卡会导致第一个选项卡的内容正确加载,从而导致其崩溃。 当我崩溃时,我没有得到堆栈跟踪,而XCode只是高光...
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([MyAppDelegate class]));
}
这不是很有帮助(如果有人可以评论如何在Xcode 5.1.1中获得堆栈区,我将很乐意使用其他信息来更新本文)。
我创建的类StatusViewController
和OperationsViewController
都是UIViewController的子类,在其中我在loadView
添加了自定义逻辑。
我在StackOverflow上看到了其他看起来相似的问题,但是它们要么使用interfacebuilder / storyboard,要么其他问题与我想问的足够不同。 期待听到任何人能为我提供的帮助。
谢谢!
这是由于内存问题(通常称为僵尸)引起的。 您没有保留UITabViewController
,因此应由UITabViewController
管理的UITabViewController
导致崩溃。
当使用由另一个UIViewController
管理的视图时,可以将它们添加到父UIViewController
的childViewControllers
数组中。
[self addChildViewController:tabBar];
[self.view addSubview: tabbar.view];
[tabBar didMoveToParentViewController:self];
苹果将此功能称为“自定义容器视图控制器” https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.