UITabBarController+UINavigationController 进入应用只显示一个tab的解决方法

来源:互联网 发布:java难学吗 编辑:程序博客网 时间:2024/05/17 06:15

各个tab所对应的controller的tabBarItem,与tabBarController的tabBar上面的item不对应的问题吧。在初始化的时候,每个viewController的title及对应的tab上图片还没有初始化好,所以获取不到,就没法显示。

脑子比较乱,就记下来吧,以后就可以直接抄了!!!

现在有下面几种写法:

写法一

NSArray *nameArray = @[@"首页", @"公告", @"月报推送", @"热点", @"我的"];NSArray *imageArray = @[@"icon_index", @"icon_notice", @"icon_push", @"icon_hot", @"icon_mine"];NSArray *selectedImageArray = @[@"icon_index_s", @"icon_notice_s", @"icon_push_s", @"icon_hot_s", @"icon_mine_s"];NSArray *viewControllerArray = @[[[HomeViewController alloc] init],                                     [[GongGaoViewController alloc] init],                                     [[MonthlyPushViewController alloc] init],                                     [[HotSpotViewController alloc] init],                                     [[MineViewController alloc] init]];ZZZBaseTabBarController *tabBarController = [[ZZZBaseTabBarController alloc] init];// 使用for循环生成各个tab对应的内容for (int index = 0; index < nameArray.count; index ++) {    UIViewController *viewController = viewControllerArray[index];    ZZZBaseNavigationController *navigationController = [[ZZZBaseNavigationController alloc] initWithRootViewController:viewController];    navigationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:nameArray[index]                                                                        image:[UIImage imageNamed:imageArray[index]]                                                                selectedImage:[UIImage imageNamed:selectedImageArray[index]]];    //设置文字偏移    [navigationController.tabBarItem setTitlePositionAdjustment:UIOffsetMake(2, -4)];    [tabBarController addChildViewController:navigationController];}self.window.rootViewController = tabBarController;[self.window makeKeyAndVisible];

其实后面的写法都一样,就是有的展开,有的用循环的区别吧

写法二

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];// 设置五个viewController,直接对viewController的tabBarItem属性赋值,同时需要设置viewController的title,否则在tabBarController初始化时,对应的这些title是取不到的,所以看不到对应的文字   HomeViewController *homeViewController = [[HomeViewController alloc] init];homeViewController.title = @"首页";homeViewController.tabBarItem.image=[UIImage imageNamed:@"icon_index"];homeViewController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_index_s"];homeViewController.tabBarItem.badgeValue=@"123";GongGaoViewController *gonggaoViewController = [[GongGaoViewController alloc] init];gonggaoViewController.title = @"公告";gonggaoViewController.tabBarItem.image=[UIImage imageNamed:@"icon_notice"];gonggaoViewController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_notice_s"];MonthlyPushViewController *monthPushViewController = [[MonthlyPushViewController alloc] init];monthPushViewController.title = @"推送";monthPushViewController.tabBarItem.image=[UIImage imageNamed:@"icon_push"];monthPushViewController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_push_s"];HotSpotViewController *hotSpotViewController = [[HotSpotViewController alloc] init];hotSpotViewController.title = @"热点";hotSpotViewController.tabBarItem.image=[UIImage imageNamed:@"icon_hot"];hotSpotViewController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_hot_s"];MineViewController *mineViewController = [[MineViewController alloc] init];mineViewController.title = @"我的";mineViewController.tabBarItem.image=[UIImage imageNamed:@"icon_mine"];mineViewController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon_mine_s"];// 五个navigationControllerZZZBaseNavigationController *homeNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:homeViewController];ZZZBaseNavigationController *gonggaoNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:gonggaoViewController];ZZZBaseNavigationController *monthPushNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:monthPushViewController];ZZZBaseNavigationController *hotSpotNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:hotSpotViewController];ZZZBaseNavigationController *mineNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:mineViewController];// 设置tabBarController和子viewController ZZZBaseTabBarController *tabBarController = [[ZZZBaseTabBarController alloc] init];tabBarController.viewControllers = @[homeNavCtrl, gonggaoNavCtrl, monthPushNavCtrl, hotSpotNavCtrl, mineNavCtrl];self.window.rootViewController = tabBarController;[self.window makeKeyAndVisible];

写法三

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];// 设置五个viewController,及其对应的tabBarItem(使用alloc的方法新建,再赋值)    HomeViewController *homeViewController = [[HomeViewController alloc] init];UITabBarItem *homeTabBarItem = [[UITabBarItem alloc] initWithTitle:@"AA" image: [UIImage imageNamed:@"icon_index"] selectedImage:[UIImage imageNamed:@"icon_index_s"]];homeViewController.tabBarItem = homeTabBarItem;homeViewController.tabBarItem.badgeValue=@"123";GongGaoViewController *gonggaoViewController = [[GongGaoViewController alloc] init];UITabBarItem *gonggaoTabBarItem = [[UITabBarItem alloc] initWithTitle:@"BB" image: [UIImage imageNamed:@"icon_notice"] selectedImage:[UIImage imageNamed:@"icon_notice_s"]];gonggaoViewController.tabBarItem = gonggaoTabBarItem;MonthlyPushViewController *monthPushViewController = [[MonthlyPushViewController alloc] init];UITabBarItem *pushTabBarItem = [[UITabBarItem alloc] initWithTitle:@"CC" image: [UIImage imageNamed:@"icon_push"] selectedImage:[UIImage imageNamed:@"icon_push_s"]];monthPushViewController.tabBarItem = pushTabBarItem;HotSpotViewController *hotSpotViewController = [[HotSpotViewController alloc] init];    UITabBarItem *hotTabBarItem = [[UITabBarItem alloc] initWithTitle:@"DD" image: [UIImage imageNamed:@"icon_hot"] selectedImage:[UIImage imageNamed:@"icon_hot_s"]];hotSpotViewController.tabBarItem = hotTabBarItem;MineViewController *mineViewController = [[MineViewController alloc] init];UITabBarItem *mineTabBarItem = [[UITabBarItem alloc] initWithTitle:@"EE" image: [UIImage imageNamed:@"icon_mine"] selectedImage:[UIImage imageNamed:@"icon_mine_s"]];mineViewController.tabBarItem = mineTabBarItem; // 五个navigationControllerZZZBaseNavigationController *homeNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:homeViewController];ZZZBaseNavigationController *gonggaoNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:gonggaoViewController];ZZZBaseNavigationController *monthPushNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:monthPushViewController];ZZZBaseNavigationController *hotSpotNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:hotSpotViewController];ZZZBaseNavigationController *mineNavCtrl = [[ZZZBaseNavigationController alloc] initWithRootViewController:mineViewController];// 设置tabBarController和子viewControllerZZZBaseTabBarController *tabBarController = [[ZZZBaseTabBarController alloc] init];tabBarController.viewControllers = @[homeNavCtrl, gonggaoNavCtrl, monthPushNavCtrl, hotSpotNavCtrl, mineNavCtrl];self.window.rootViewController = tabBarController;[self.window makeKeyAndVisible];
阅读全文
0 0
原创粉丝点击