ios - 如何根据我的 json 响应数组制作标签栏(ios swift)

标签 ios swift swift2 uitabbarcontroller ios9

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

相关文章:

ios - 使用 UIActivityViewController 共享电子表格、视频等

ios - 如何快速获取指向 View Controller 的全局指针

ios - TableView 不显示 Cells Swift

ios - 如何使用 UIBezierPath 使用 drawInRect 绘制 UIImage?

ios - 将 json 对象从 iphone 发送到 java servlet 的时间太长

objective-c - 如何找到事件 AirPlay 设备的 IP 地址?

ios - NSFileManager.defaultManager().createFileAtPath() 返回 false

php - 使用字符串参数创建开关不起作用

ios - 在 IOS 应用程序中编辑和保存文本

ios - FacebookSDK(4.7) 自定义登录 UI 按钮 - Swift 2.0