how to make tabbar based on my json response array (ios swift)

前提是你 提交于 2019-12-01 14:27:08

Number of tabs in UITabbarController depends on number of ViewControllers/NavigationControllers we are adding in tabbar.

Depending on the count of service response, you can change the number of viewcontrollers in tabbar at runtime. https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/#//apple_ref/occ/instp/UITabBarController/viewControllers

/*
tabs =     (
            {

                id = 0;
                name = Home;

            },
            {
                /...
            }
            )
*/

- (void) loadTabbarsWithArray:(NSArray*)tabs{

    if (self.tabBarController == nil) {
        self.tabBarController = [[UITabBarController alloc] init];
    }
    self.tabBarController.viewControllers = [NSArray array];

    NSMutableArray *viewControllers = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *record in tabs) {
        UIViewController *viewController = [[UIViewController alloc] initWithNibName:"CustomViewController" bundle:nil];
        viewController.title = record[@"name"];
        viewController.tabBarItem.title = record[@"name"];
        [viewControllers addObject:viewController];
    }
    [self.tabBarController setViewControllers:viewControllers];

}

in Swift

func loadTabbarsWithArray(let tabs:[[String: Any]]){

        if (self.tabBarController == nil) {
            self.tabBarController = UITabBarController();
        }
        tabBarController!.viewControllers = [UIViewController]();

        var viewControllers = [UIViewController]();
        for  record:[String: Any] in tabs {
            let viewController:UIViewController = UIViewController(nibName: "CustomViewController", bundle: nil);
            viewController.title = record["name"] as? String;
            viewController.tabBarItem.title = record["name"]as? String;
            viewControllers.append(viewController);
        }
        tabBarController!.viewControllers = viewControllers;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!