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

前端 未结 1 1962
夕颜
夕颜 2021-01-16 22:30

Problem : I want to create tab bar based on my JSON response array, this means, if I got 6 elements in response it will create 6 tabs.

Tried : I already made it by u

相关标签:
1条回答
  • 2021-01-16 22:57

    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;
        }
    
    0 讨论(0)
提交回复
热议问题