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

各个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"];

// 五个navigationController
ZZZBaseNavigationController *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;

 // 五个navigationController
ZZZBaseNavigationController *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];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值