how to make tabbar based on my json response array

2019-02-21 01:22发布

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 using horizontal scrolling collection view but I want to make it by original tab bar.

So, how can I do this?

please tell me the possible solutions and dont put this on hold..

this is my response so how can i do with this?

tabs =     (
            {

        id = 0;
        name = Home;

    },
            {

        id = 1;
        name = Winkel;

    },
            {

        id = 2;
        name = Zoeken;

    }

);
})

thanks @Ankit for swift code but when used your code and passing array with named "arr" getting this error Cannot convert value of type 'NSMutableArray' to expected argument type '[[String : Any]]'

here is my code

func web()
{

    request(.GET, "http://www.horecasupply.nl/AJAX?function=appStructure", parameters: nil, encoding: .JSON).responseJSON { (response:Response<AnyObject, NSError>) -> Void in
        print(response.result.value)

        if (response.result.value != nil)
        {
            self.arr = (response.result.value)!["tabs"] as! NSMutableArray

            print(self.arr)
        }


        loadTabbarsWithArray(arr)

    }

and above you can show my json response so how can i solve it

1条回答
ら.Afraid
2楼-- · 2019-02-21 02:17

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;
    }
查看更多
登录 后发表回答