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 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...