Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何基于我的json响应数组(ios swift)制作tabbar_Ios_Swift_Swift2_Uitabbarcontroller_Ios9 - Fatal编程技术网

如何基于我的json响应数组(ios swift)制作tabbar

如何基于我的json响应数组(ios swift)制作tabbar,ios,swift,swift2,uitabbarcontroller,ios9,Ios,Swift,Swift2,Uitabbarcontroller,Ios9,问题:我想基于JSON响应数组创建选项卡栏,这意味着,如果响应中有6个元素,它将创建6个选项卡 尝试:我已经通过使用水平滚动集合视图实现了,但我想通过原始选项卡栏实现 那么,我该怎么做呢 please tell me the possible solutions and dont put this on hold.. 这是我的回答,我该怎么做 tabs = ( { id = 0; name = Home; },

问题:我想基于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;

    }

);
})
感谢@Ankit提供swift代码,但当使用您的代码并传递名为“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)

    }
func-web()
{
请求(.GET,”http://www.horecasupply.nl/AJAX?function=appStructure,参数:nil,编码:.JSON).responseJSON{(response:response)->Void in
打印(响应.结果.值)
if(response.result.value!=nil)
{
self.arr=(response.result.value)![“tabs”]as!NSMutableArray
打印(self.arr)
}
负载选项卡Barswitharray(arr)
}

在上面,您可以显示我的json响应,因此我如何解决它呢?UITabbarController中的选项卡数量取决于我们在tabbar中添加的ViewController/NavigationController的数量

根据服务响应的计数,您可以在运行时更改选项卡栏中ViewController的数量。

迅速地

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

谢谢你的建议,我重写了我的问题,你能告诉我怎么做吗?@Ankitthanx@Ankit在使用你的代码时,我如何调用这个函数。因为当我像loadTabbarsWithArray(arr)一样调用它时,它的showing me错误无法将“NSMutableArray”类型的值转换为预期的参数类型“[[String:Any]]。这是我的数组内容arr=({id=0;name=Home;},{id=1;name=Winkel;},{id=2;name=Zoeken;})
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;
    }