以编程方式创建uiTabBarController

Meh*_*hdi 10 objective-c uitabbarcontroller ios

我想创建UIView一个UITabBarController

这是我的.h文件代码:

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

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;
Run Code Online (Sandbox Code Playgroud)

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

Thread1:程序接收信号:SIGABRT

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

ms8*_*s83 0

尝试改变

self.view = tabBarController.view;

[self.view addSubview:tabBarController.view];

看看是否有帮助。

也尝试将其放入您的-(void)loadView方法中

- (void)loadView {

    UIView *mv = [[UIView alloc] initWithFrame:CGRectMake(0.0, 100.0, 320.0, 480.0)];

    self.view = mv;

    [mv release];
}
Run Code Online (Sandbox Code Playgroud)

您遇到黑屏的原因可能是因为您没有正确初始化 UIView。