ios – 以编程方式创建uiTabBarController

我想为UITabBarController创建一个UIView

这是我的.h文件的代码:

@interface TE : UIViewController <UITabBarControllerDelegate>{
    UITabBarController *tabBarController;
}
@property (nonatomic,retain) UITabBarController *tabBarController;
@end

viewDidLoad方法:

UIViewController *testVC = [[T1 alloc] init];
UIViewController *otherVC = [[T2 alloc] init];
NSMutableArray *topLevelControllers = [[NSMutableArray alloc] init];
[topLevelControllers addObject: testVC];
[topLevelControllers addObject: otherVC];
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
[tabBarController setViewControllers:topLevelControllers animated:NO];
tabBarController.selectedIndex = 0;
self.view = tabBarController.view;

这会创建标签栏控制器,但是当我点击标签栏项目时,我会收到一个错误:

Thread1:Program receive signal: SIGABRT

编辑:我通过下载并修改了http://www.iphonedevcentral.com/create-uitabbarcontroller/版本来解决了这个问题

最佳答案
你上面说的你不想在appDelegate中创建tabBarController.为什么不?你会在哪里创建它? tabBarController必须是根视图控制器,不能是任何其他视图控制器的子级.

Btw,确保你实现:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    NSUInteger tabIndex = [tabBarController.viewControllers indexOfObject:viewController];

    if (viewController == [tabBarController.viewControllers objectAtIndex:tabIndex] ) {
         return YES;
    }

    return NO;

}

转载注明原文:ios – 以编程方式创建uiTabBarController - 代码日志