以编程方式创builduiTabBarController
我想为UITabBarController
创build一个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;
这将创build标签栏控制器,但是当我点击一个标签栏项时,出现错误:
线程1:程序接收信号:SIGABRT
编辑 :我解决了这个问题,下载并修改http://www.iphonedevcentral.com/create-uitabbarcontroller/的版本
你上面说,你不想在appDelegate中创buildtabBarController。 为什么不? 你还会创造它吗? tabBarController必须是根视图控制器,不能是任何其他视图控制器的子视图。
顺便说一句,确保你执行:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { NSUInteger tabIndex = [tabBarController.viewControllers indexOfObject:viewController]; if (viewController == [tabBarController.viewControllers objectAtIndex:tabIndex] ) { return YES; } return NO; }
-
子类UITabBarController
-
覆盖 – (void)loadView方法并包含下面的代码
MyCustomViewControllerOne* ctrl1 = [[[MyCustomViewControllerOne alloc] initWithNibName@"MyViewControllerOne" bundle: nil] autorelease]; UIViewController* ctrl2 = [[[UIViewController alloc] init] autorelease]; MyCustomControllerTwo* ctrl3 = [[[UIViewController alloc] initWithObject: myObj] autorelease]; ctrl1.title = @"First tab"; ctrl2.title = @"Second tab"; ctrl3.title = @"Third tab"; ctrl1.tabBarItem.image = [UIImage imageNamed:@"tab_image1.png"]; ctrl2.tabBarItem.image = [UIImage imageNamed:@"tab_image2.png"]; ctrl3.tabBarItem.image = [UIImage imageNamed:@"tab_image3.png"]; [self setViewControllers: @[ctrl1, ctrl2, ctrl3]];
这是非常多的。
更改self.view = tabBarController.view;
至
[self.view addSubview:tabBarController.view];
它正常工作
试着改变
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]; }
您可能遇到黑屏的原因是您没有正确初始化您的UIView。
@ Mehdi,只要让你的TE UITabBarController而不是一个UIViewController然后有一个TabBarController在其中。 使pipe理TabBarController更容易。 为了响应其他已经表明只能有一个TabBarController作为窗口的rootViewController的其他人。 事实并非如此。 UITabBarController可以在需要二级菜单导航的多个地方实例化。 在TabBar中有一个TabBar是没有意义的,但有一个左导航菜单,然后在每个菜单项上有一个TabBar将是有道理的。