问题:我想基于 JSON 响应数组创建选项卡栏,这意味着,如果我得到 6 个元素响应,它将创建 6 个选项卡。
尝试过:我已经通过使用水平滚动 Collection View 来制作它,但我想通过原始选项卡栏来制作它。
那么,我该怎么做呢?
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提供快速代码,但是当使用您的代码并传递名为“arr”的数组时出现此错误无法将类型“NSMutableArray”的值转换为预期参数类型“[[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 中的选项卡数量取决于我们在选项卡栏中添加的 ViewControllers/NavigationControllers 数量。
根据服务响应的数量,您可以在运行时更改选项卡栏中 View Controller 的数量。 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;
}
关于ios - 如何根据我的 json 响应数组制作标签栏(ios swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34868697/