[英]how to make tabbar based on my json response array (ios swift)
問題:我想基於我的JSON響應數組創建選項卡欄,這意味着,如果響應中有6個元素,它將創建6個選項卡。
嘗試過:我已經通過使用水平滾動收集視圖來做到這一點,但是我想通過原始的標簽欄來實現。
那么,我該怎么做呢?
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;
}
);
})
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
這是我的代碼
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中的選項卡數量取決於我們在選項卡欄中添加的ViewController / NavigationControllers的數量。
根據服務響應的數量,您可以在運行時在選項卡欄中更改視圖控制器的數量。 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];
}
在斯威夫特
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.