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
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;
}