如何使基于我的JSON响应数组的tabbar(ios swift)

问题:我想创build基于我的JSON响应数组的标签栏,这意味着,如果我有6个元素作为响应,它将创build6个选项卡。

试过了:我已经通过使用水平滚动collections视图,但我想通过原来的标签栏。

那么,我该怎么做呢?

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

这是我的回应,我该怎么办?

 tabs = ( { id = 0; name = Home; }, { id = 1; name = Winkel; }, { id = 2; name = Zoeken; } ); }) 

感谢@Ankit为swift代码,但是当您使用您的代码和传递名为“arr”的数组得到此错误无法将types'NSMutableArray'的值转换为期望的参数types'[[String:Any]]'

这是我的代码

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

和以上,你可以显示我的JSON响应,所以我如何解决它

UITabbarController中的选项卡数量取决于我们在Tabbar中添加的ViewControllers / NavigationControllers的数量。

根据服务响应的数量,您可以在运行时更改tabbar中的视图控制器数量。 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]; } 

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