ios – 以编程方式创建uiTabBarController

前端之家收集整理的这篇文章主要介绍了ios – 以编程方式创建uiTabBarController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为UITabBarController创建一个UIView

这是我的.h文件代码

  1. @interface TE : UIViewController <UITabBarControllerDelegate>{
  2. UITabBarController *tabBarController;
  3. }
  4. @property (nonatomic,retain) UITabBarController *tabBarController;
  5. @end

viewDidLoad方法

  1. UIViewController *testVC = [[T1 alloc] init];
  2. UIViewController *otherVC = [[T2 alloc] init];
  3. NSMutableArray *topLevelControllers = [[NSMutableArray alloc] init];
  4. [topLevelControllers addObject: testVC];
  5. [topLevelControllers addObject: otherVC];
  6. tabBarController = [[UITabBarController alloc] init];
  7. tabBarController.delegate = self;
  8. [tabBarController setViewControllers:topLevelControllers animated:NO];
  9. tabBarController.selectedIndex = 0;
  10. self.view = tabBarController.view;

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

Thread1:Program receive signal: SIGABRT

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

解决方法

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

Btw,确保你实现:

  1. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  2.  
  3. NSUInteger tabIndex = [tabBarController.viewControllers indexOfObject:viewController];
  4.  
  5. if (viewController == [tabBarController.viewControllers objectAtIndex:tabIndex] ) {
  6. return YES;
  7. }
  8.  
  9. return NO;
  10.  
  11. }

猜你在找的iOS相关文章